0

I've been attempting the Cash problem. There are no errors when I enter "make cash". But when I enter "./cash", it asks "How much change in dollars is owed?", I answer "0.41", then nothing happens. Where did I go wrong here?

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

int main(void)
{
    float f = get_float("How much change in dollars is owed?\n");
    //ask how much change is owed
    int cents = round(f * 100);
    //convert user input from dollars to cents
    for (int i = 0; cents > 0 ; i++) 
    // monitor no. of coins used with i
    {
        if (cents >= 25)
        {
            cents = cents - 25;
        }
        else if (cents >= 10)
        {
            cents = cents - 10;
        }
        else if (cents >= 5)
        {
            cents = cents - 5;
        }
        else if (cents >= 1)
        {
            cents = cents - 1;
        }
        else if (cents == 0)
        {
            printf("%i\n", i);
        }
    }
}

P.S. I originally did not convert float to int and it ended up with what I think was an infinite loop. I assume because float becomes inaccurate as you go further into the decimals so cents never became == 0 and therefore was not able to printf("%i\n", i). Would also like to know if this is the case here.

Thanks in advance for any help.

1

The condition to enter the loop body is set to cents > 0. This means cents == 0 can never be true.

You must log in to answer this question.

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