1

My program for pset1 (greedy) compiles fine and runs. However, it just prompts the user for the amount of change. After that, a result to show the number of coins never gets printed out, and the program never stops running. It seems like there is a problem with my while loops, but I can't figure it out.

Her is the code for my greedy program:

int main(void)
{

float change;

int coins = 0;

    do
    {
        change = get_float("Change owed: ");
    }
    while (change < 0);

    change *= 100;
    change = round(change);
    //Converts dollars into cents, and rounds the number

    while (change >= 25)
    ; // This loop is for quarters
    {
        coins++;
        change -= 25;
    }

    while (change >= 10)
    ; // This loop is for dimes
    {
        coins++;
        change -= 10;
    }

    while (change >= 5)
    ; // This loop is for nickels
    {
        coins++;
        change -= 5;
    }

    while (change >= 1)
    ; // This loop is for pennies
    {
        coins++;
        change -= 1;
    }

    printf("%i\n", coins);
}

Thanks in advance for answering my question.

3
while (change >= 25)
    ;

means "while change is greater than or equal to 25, do nothing". So your programme sits there repeatedly doing nothing, in an infinite loop.

The semicolon ; is the empty statement, representing a NOP (no operation). In Python, the equivalent would be pass. Another way to express the same is the empty block {}, which I prefer for readability.

style50 wants you to put that single semicolon on its own line to make it clear that it does not belong to the line before, but is its own statement.

So the fix is simple - remove those semicolons after the while loops.

1
  • Thank you very much. Everything works perfectly now.
    – Aisha
    May 30 '18 at 10:02

You must log in to answer this question.

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