Search code examples
cwhile-loopsyntax-errorcs50logical-and

cs50 pset1 cash.c expected expression


I finished writing my code for cash.c, but I'm unable to compile it. There's an 'expected expression' error for every one of my < at the //least amount of coins to make a certain amount of change section. I tried changing it a couple of times, and even look up what the hell I'm doing wrong but I can't find anything. Can someone please help me? Also, I'd love to hear any advice or feedback about my code! The code is down below.

Thank you! Aleena <3

#include <cs50.h>
#include <stdio.h>
#include <math.h>

int main(void)
{
    // prompt user for non-negative input & convert dollars to cents
    float change;
    int cents;
    do
    {
        change = get_float("How much change? ");
        cents = round(change * 100);
    }
    while (change < 0);

    //least amount of coins to make certain amount of change
    int coins;
    coins = 0;
    while (cents >= 0.25)
    {
        coins++;
        cents = cents - 25;
    }
    while (cents >= .1 && < 0.25)
    {
        coins++;
        cents = cents - 10;
    }
    while (cents >= .05 && < .1)
    {
        coins++;
        cents = cents - 5;

    }
    while (cents >= 0.01 && < .05)
    {
        coins++;
        cents = cents - 1;
    }
    printf("%i\n", coins);
}

I'm also wondering if it's normal that I'm having a hard time with cs50? I understand everything in the lectures and shorts, but the problem sets seem to be taking me a very long time. It took me like 3 weeks to complete mario.c, and I can't do them without Googling something. It's making me question if I should even be listening to this course without any experience. I'm really enjoying this course, but do you think I should take it down a notch and start with something a bit more beginner friendly?


Solution

  • For starters such a condition

     while (cents >= 0.25)
    

    in any case does not make a sense because the variable cents is declared as having the type int.

    int cents;
    

    It seems you mean

     while (cents >= 25 )
    

    In other while loops you have a syntax error as for example in this while loop

    while (cents >= .1 && < 0.25)
    

    You need at least to write

    while (cents >= 10 && cents < 25)
    

    But when the control reaches this while loop the variable cents already has a value that is less than 25 due to the preceding while loop. So it is enough to write

    while ( cents >= 10 )
    

    So your loops can look like

    while ( cents >= 25 )
    {
        coins++;
        cents = cents - 25;
    }
    while ( cents >= 10 )
    {
        coins++;
        cents = cents - 10;
    }
    while ( cents >= 5 )
    {
        coins++;
        cents = cents - 5;
    
    }
    while (cents >= 1 )
    {
        coins++;
        cents = cents - 1;
    }