Ian M.
Ian M.

Reputation: 11

MIT OpenCourseWARE C/C++ Assignment 1 no output issue

I know very little about C/C++ so I decided to go through MIT's OpenCourseWare class online. I did two years of Java classes about 8 years ago and I have about 4 years of recent experience with MATLAB.

After struggling to get the compiler setup, I manage to get the first lab completed in Eclipse IDE. I can run both Hello World codes in Eclipse or cmd and they will output whatever text I have in the code. I'm on 'Assignment 1' and I've made the modifications in the answer key to the .C files.

Here is the link to the assignment: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-s096-introduction-to-c-and-c-january-iap-2013/lectures-and-assignments/compilation-pipeline/

It appears to compile and generate the .exe, but when I go to the command prompt to enter the two inputs (int argc, char *argv[]), it runs but generates no output. I'm using a command as suggested on the website: ./fibeverse 6 'what a trip that was!'. There is only one warning in Eclipse, it is in fibverse.c and it says "unused variable 'i'" despite the variable being used in the two 'if' statements below.

I am using this line for my makefile: gcc -Wall -std=c99 fibeverse.c reverse.c fibonacci.c -o fibeverse

Does anyone know if I may be doing something incorrectly? I'm not sure if the error is how I am calling the .exe file or if the .exe was generated incorrectly.

I can post my exact text if desired, I don't want to clutter the post unless it's necessary. I've reviewed the solution key several times to make sure I haven't mistyped anything.

Thanks for your time,

Ian

Edit: Here is the main, it doesn't post very nicely in the comments:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "fibonacci.h"
#include "reverse.h"

int main(int argc, char *argv[]) {
int i = 1;

#ifdef fibonacci
if (i < argc) {
    print_fib(atoi(argv[i]));
    i++;
}
#endif

#ifdef reverse
if (i < argc) {
    reverse(argv[i], strlen(argv[i]));
    i++;
}
#endif
printf("Press Enter to Exit\n");
getchar();

return 0;
}

The printf was added just to make sure it wasn't flashing the output, it responds no differently without it. Also, the #ifdef were just changed to lowercase, but that didn't change anything either.

Thanks for the fast responses!

Upvotes: 0

Views: 251

Answers (3)

Eric Postpischil
Eric Postpischil

Reputation: 223513

Assignment 1 has two problems. In problem 1, the program should be compiled with gcc -Wall -std=c99 fibeverse.c reverse.c fibonacci.c -o fibeverse, and the errors that prevent the program from compiling should be fixed.

There are two errors:

  • Each of fibonacci.c and reverse.c does not include its corresponding header, fibonacci.c and reverse.c. While a source file is not required to include its corresponding header, it is usually good practice for two reasons. One, the header declares routines that the source file defines, and including the header allows the compiler to see both declarations and definitions in the same compilation, which in turn allows the compiler to report inconsistencies between the two. Two, source files often use some of their own routines as subroutines, and including the header provides declarations for those routines, which are needed for routines that are used before they are defined. In fibonacci.c, fib is used before it is defined, so it should have a declaration before its use. Including fibonacci.h provides this declaration.
  • reverse.c defines a routine named backwards but is intended to define a routine named reverse, so the name should be changed.

Once these errors are fixed, the program compiles, without error, using the command shown above.

That command is only for problem 1 of the assignment and should not be used for problem 2.

In problem 2, you modify the program so that the two pieces of code for calling print_fib or reverse can be separately enabled or disabled. As you have seen in the provided solution, this can be done by inserting statements such as #ifdef FIBONACCI.

Once you have made this modification, you enable each piece of code by including the switch -DFIBONACCI or -DREVERSE in the compiler command line. Once you have made this modification, the original command line is not expected to work without complaint. Do not use it.

The instructions for problem 2 show three commands to be used with the command after the program is modified.

This command builds the program so it only computes the Fibonacci number:

gcc -Wall -std=c99 -DFIBONACCI fibeverse.c reverse.c fibonacci.c -o fibonacci

This command builds the program so it only reverse the string:

gcc -Wall -std=c99 -DREVERSE fibeverse.c reverse.c fibonacci.c -o reverse

This command builds the program so it does both:

gcc -Wall -std=c99 -DFIBONACCI -DREVERSE fibeverse.c reverse.c fibonacci.c -o fibeverse

Upvotes: 0

kbg
kbg

Reputation: 1

I read the assignment. There are two C pre-processor constructs that you need to learn.

#ifdef foo
// Compile everything in this block if flag "foo" is defined in the program 
// or in the make file.
#end

#ifndef foo
// Compile everything in this block if flag "foo" is NOT defined anywhere.
#end

You can fix the compile time warning by replacing #ifdef with #ifndef. When you proceed to part two of the assignment, mix and match these constructs with compile time flags.

Upvotes: -1

DreemKiller
DreemKiller

Reputation: 114

It's saying the variable is unused because it is. You've got ifdefs around both blocks of code where the variable i is used, but you are not defining the macros that are being tested. At the top of the file, add:

#define fibonacci 1
#define reverse 1

You will see that the warning message disappears. You will also find that the for loops are now being executed.

Upvotes: -1

Related Questions