0

Please help! I cant solve some errors (from: [http://docs.cs50.net/problems/greedy/greedy.html][1]) running this code:

int main(void) 
{
    float change = 0;
    int coin_used = 0;
    int change2 = 0;
    printf("Change owed:\n");
    change = get_float();

    while (change <= 0)
    {
        printf("Enter a valid number. Change owed:");
        change = get_float();
    }
    change2 = change * 100;

     while (change2 != 0)
    {
        if (change2%25 != change2)   
            change2 %= 25;
            coin_used++;

        else if(change2%10 != change2);   
            change2 %= 10;
            coin_used++;

        else if(change2%5 != change2)   
            change2 %= 5;
            coin_used++;

        else (change2%1 != change2);   
        change2 %= 1;
        coin_used++;  
     }
    printf("Modulo: %i\n", change2);//I momentarily created this to see the output
    printf("change %f\n", change);
    printf("coin used %d\n", coin_used);
    printf("change2 %i\n", change2);
}

And I got this output:

greedy.c:25:9: error: expected expression

    else if(change2%10 != change2);

greedy.c:30:9: error: expected expression

    else if(change2%5 != change2)

greedy.c:35:9: error: expected expression

    else (change2%1 != change2);
0

The error is created by the compiler finding an else clause without knowing the corresponding if (there has to be exactly one statement or code block in between).

Use {} blocks with your if to conditionally execute several statements, and never ever place a semicolon behind your if condition, that's the empty statement, you essentially tell the compiler to conditionally do nothing, and proceed with the next line.

You must log in to answer this question.

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