0

I'm just getting into C and I am having some troubles. I'm having a heck of a time figuring out why this while loop won't repeat. I made the same loop in JavaScript and it repeats out the proper output. http://jsfiddle.net/rFghh/

If I use while (cents >= 25) then the terminal prints starting coins and hangs just blinking. If I use <=25 (as below) it prints one iteration. Any idea's on what i'm doing wrong??

/**
 * Greedy, given a change amount, figures out the min number of coins needed
 */

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

int main(int argc, char const *argv[])
{
    // declare variables
    int cents = 0;
    int coins = 0;

    // ask user for amount of money, accepts a float value
    // convert input to cents and round to nearest int
    printf("O hai! ");
    do 
    {
        printf("How much change is owed? ");
        cents = round(100 * GetFloat());
    // if input is negative then ask again
    } while (cents <= 0);

    printf("Starting Cents: %d, coins are: %d\n", cents, coins);

    // Check if you can use a quarter
    while (cents <= 25);
    {
        printf("Cents before decrement: %d\n", cents);
        cents = cents - 25;
        printf("Cents after decrement: %d\n", cents);
        coins = coins + 1;
    }

    printf("FINAL Cents: %d, coins are: %d\n", cents, coins);

    return 0;
}



jharvard@appliance (~/cs50/Week_1/pset1): make greedy && ./greedy
clang -ggdb3 -O0 -std=c99 -Wall -Werror    greedy.c  -lcs50 -lm -o greedy
O hai! How much change is owed? 1.25
Starting Cents: 125, coins are: 0
Cents before decrement: 125
Cents after decrement: 100
FINAL Cents: 100, coins are: 1
jharvard@appliance (~/cs50/Week_1/pset1): 
4
  • 1
    Remove the ; right after while(...). Commented Jan 18, 2013 at 20:17
  • 4
    You're heading to a copy of K&R C. Now. Commented Jan 18, 2013 at 20:17
  • @H2CO3 , would you recommend K&R C over C the hard way? BTW, the extra semi-colon was a typo =) Commented Jan 18, 2013 at 20:28
  • @SkinnyG33k I'd primarily recommend googling "C tutorial". Commented Jan 18, 2013 at 20:29

3 Answers 3

8

The code does not do what you think it does. This line:

while (cents <= 25);
{ ::: }

is equivalent to this:

while (cents <= 25)
{
    ;
}
{ ::: }

So that will iterate forever executing an empty-statement that never changes cents. Fix it by removing the semicolon, and by reevaluating your logic.

Sign up to request clarification or add additional context in comments.

3 Comments

Well, it will iterate never or infinitely, because the value of cents is not changed in the loop.
Also, the logic is flawed anyway in the OP's code. He essentially wrote "while cents is less than 25, decrement it". Makes no sense whatsoever.
Ahh... typo...thanks! I was wondering why it wasn't printing anything! @H2CO3 , correct, it was originally 'while cents is more than 25' but it just hung, and printed once when it was lte 25.
7

You have a semi-colon at the end of while statement: -

while (cents <= 25);  <-- Here's the semi-colon. Remove it.

Comments

1

Your check for quarters needs to be fixed. This should actually probably be a separate function, but the quick fix would be:

while (cents >= 25)  //should be greater than or equal to and get rid of semicolon 
{
    printf("Cents before decrement: %d\n", cents);
    cents = cents - 25;
    printf("Cents after decrement: %d\n", cents);
    coins++;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.