0

For some reason, even though I have declared the float variable change in my greedy.c program, the Clang compiler gives an error message saying that I have not declared it.

Even with 3 compiler errors my program appears to pass check50 test.

Output:

jharvard@appliance (~/Dropbox/pset1): make greedy12
clang -ggdb3 -O0 -std=c99 -Wall -Werror    greedy12.c  -lcs50 -lm -o greedy12
greedy12.c:6:4: error: expected identifier or '('
   {
   ^
greedy12.c:28:20: error: use of undeclared identifier 'change'
    int i = round (change*100);
                   ^
greedy12.c:33:4: error: expected identifier or '('
   {    
   ^
3 errors generated.
2

Your code is actually full of syntax errors. I'm gonna walkthrough these syntax errors leaving the logical errors for you to figure out.

  1. There's no semicolon after main()'s signature.

    int main(void) // no semicolon here
    {
        // do something
    }
    

It's also recommended that when you have a list of error thrown by the compiler, you start fixing the first one because usually, the first one makes the compiler think that there are other errors.

  1. you shouldn't be putting an opening parentheses (i.e., () before float change;.

  2. A semicolon is missing after change = GetInt().

  3. You shouldn't be putting a comma after (change > 0) and you shouldn't enclose the if statement with parentheses. Also, it's recommended that you contain the code that's gonna be executed when the condition is true (aka the body of the condition) within curly braces.

    if (change > 0)
    {
        // do something
    }
    

    Same thing applies for the else statement

    else
    {
        // do something else
    }
    

I'm not able to see the rest of the code since you didn't post it, but I highly recommend you watch the resources of week 1 again, taking notes of how to define main(), how to create an if/else statement, how to create a loop and so on.

Lastly, it's impossible that check50 will let that pass because it won't compile!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .