0

So if a pass a float like 0.41 i receive "4", as expected. And that's work with any float number. But if i pass an integer like "2" or even "2.00", program stuck and ask for input again. Here is the screenshot: enter image description here

And a bit of my source code:

int main (void)
{
printf("O hay! How much change is owed?\n");
int coins = 0;
float money = GetFloat();
//printf("%f\n", money);


if ( money > 0 ) {

    while ( money < 0 ) {
        printf("Retry: ");
        money = GetFloat();
   }

    int moneyInt = floor(round(money*100));

    //printf("%d\n", moneyInt);


    while ( moneyInt > 0 ) {

        if ( moneyInt > 25 ) {
            moneyInt -= 25;
            coins++;
        }
        else if ( moneyInt < 25 && moneyInt >= 10 ) {
            moneyInt -= 10;
            coins++;
        }
        else if ( moneyInt < 10 && moneyInt >= 5 ) {
            moneyInt -= 5;
            coins++;
        }
        else if ( moneyInt < 5 && moneyInt >= 1 ) {
            moneyInt -= 1;
            coins++;
        }
    }

}

help. plz.

3
  • Some pointers, you don't need if ( money > 0 ) since it won't leave the while loop until money > 0 any way also you don't need moneyInt < 25 && since you use else if.
    – Aboalnaga
    Dec 16 '15 at 15:45
  • OMG! removing moneyInt < 25 && from first else if helped!!! Magic! I still have no idea how it worked =) but THANK YOU vary much! Dec 16 '15 at 15:50
  • as I pointed in my answer you are missing a case, what if you have 0.25 remaining, which if condition should execute.
    – Aboalnaga
    Dec 16 '15 at 15:55
0

You are missing a case and stuck in a loop, check what will happen when the value is 0.25.

You must log in to answer this question.

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