0

enter image description here

-- hi im running into a problem with my code. It's executed pretty well with many inputs except for 0.41 and 0.01. It didn't print out any answer but just ran indefinitely...

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


int main(void) {
    float money;
    do { 
        money = get_float("Change owed: ");
    }
    while (money < 0.00);  
    int x = round(money * 100);
    int c = 0;
    while (x >= 25) {
        c = c + floor(x / 25);
        x = x % 25; 
    }
    while (x >= 10) {
        c = c + floor(x / 10);
        x = x % 10; 
    }
    while (x >= 5) {
        c = c + floor(x / 5);
        x = x % 5;
    }
    while (x >= 1) {
        c = c + floor(x / 1);
    }
    printf("%i\n", c);
}
0

The reason you are having infinite loops is because if you have c = 1,

then if you pass it in your while loop it will be true infinitely since you never change it after that.

Also your loops are unnecessary just remove them since you don't need multiple iterations to divide a number, 100 / 25 = 4; so after 1 iteration the condition will be false same as to all your other loops except for the user input do while loop.

You must log in to answer this question.

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