0

I'm on pset1 of CS50, trying to solve greedy.c. This is my code, which I think it's kind of good:

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

int main(void)
{
    int quarter = 0, dime = 0, nickle = 0, penny = 0;
    float change;

    do
    {
        printf("O hai! How much change is owed?\n");
        change = GetFloat();
    }
    while (change <= 0);

    while (change != 0)
    {
        if (change > .25)
        {
            quarter += 1;
            change -= .25;
        }
        else if (change > .10)
        {
            dime += 1;
            change -= .10;
        }
        else if (change > .05)
        {
            nickle += 1;
            change -= .05;
        }
        else
        {
            penny += 1;
            change -= .01;
        }
    }

    printf("Your change is: %d quarters, %d dimes, %d nickles, %d pennies.", quarter, dime, nickle, penny);

    return 0;
}

I just have a slight problem with the do-while loop though:

do
    {
        printf("O hai! How much change is owed?\n");
        change = GetFloat();
    }
    while (change <= 0);

For some reason, when I type in a number, the console keeps asking me for input. Please help me, I'm so confused, and this code works just fine when declaring change an integer instead of a float and using the GetInt() function.

Thanks in advance :)

1

Welcome to the whole point of this pset. You are operating on mixed types, floats and ints. Because of the way a float is stored in a computer, a number with a fractional part ( a number with digits to the right of the decimal) is rarely stored exactly. Instead, it is a very close approximation.

Because of this, as you operate on change, it will never be exactly 0, but very, very close - maybe something like 0.000000000357 (your mileage may vary.) When you do the test in while(change != 0) it will never be true because change will not be EXACTLY zero!

You need to figure out a way to convert change to an int in a form that you can readily use to do integer arithmetic.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

5
  • The problem is with the first loop, (change <= 0). When the terminal asks me for a float and I type in a float that , as in 3.4, the program sort of locks and keeps asking for input, but somehow it doesn't get out of the GetFloat() function. If I reboot the program and I instead type a 0 or a value less than 0 it does go through the loop. Please can you try to run this, I'm starting to think I'm crazy because whenever I type in a value that is greater than 0 the program goes into some kind of lock mode.
    – Andrés
    Mar 17 '16 at 23:20
  • Currently having severe interface corruption problems with my IDE, so I can't run anything. However, I'm highly suspicious. The code you posted should work fine for the getFloat while loop. Is it possible that the code you are actually running says while (change >= 0) instead of <=??? If it does, it would explain the behavior.
    – Cliff B
    Mar 18 '16 at 3:37
  • No, it says while (change <= 0). And the weirdest thing is it doesn't even work when replacing change = GetFloat(); with scanf("%f", &change); for some reason. If I enter a number less than or equal to zero, it does the expected, (loops again and asks, prints O hai! How much change is owed? and asks the user for input. But if I type in a number greater than zero, it locks and never stops asking for more input (I know this because it never prints "O hai! How much change is owed?" again), and the only way to stop the input scanning is to terminate the program.
    – Andrés
    Mar 18 '16 at 11:55
  • I ran your code above and it behaved exactly as I expected. The first while loop correctly prompted for input, but when valid input is received, it goes into an infinite loop because of the problem with the second while loop that I described. It would appear locked up in your display because it is processing internally, but not printing anything to the screen. If you want to see something, put printf("ping\n"); inside the start of the second while loop. (continued...)
    – Cliff B
    Mar 18 '16 at 17:21
  • (...continued) As for the behavior you described, if it is reprompting after valid input, is it possible that you are not running the current compiled version? Maybe you're saving to one directory and are running an old version in another? Maybe it isn't compiling and you're running an old build? I can't see exactly what's happening on your system, but if you are running the code posted above, and it is correctly compiled, then it can't be looping on the first loop with valid input. If it's a bad compile, try deleting the executable file and recompiling to guarantee that it's good.
    – Cliff B
    Mar 18 '16 at 17:24

You must log in to answer this question.

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