0

I'm trying to solve the less comfortable problem set of week1.

I believe I'm somewhat on the correct road (at least i can get a return number). From how I implemented my code, it seems that when I input the "change" float value, the while loops pass at least once but they don't reduce the "change" value correctly after each passage. I'm pasting my code for clarification.

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


int main (void)
{   

    float quarter = 0.25;
    float dime = 0.10;
    float nickel = 0.05;
    float penny = 0.01;
    int count = 0;
    float change;
    do {
     printf ("O hai! How much change is owed?\n");   
     change = get_float();
    }
    while (change <0);

    while (change>quarter){
        count++;
         change = change - quarter;
        break;
    }
    while (change>dime) {
        count++;
         change= change - dime;
         break;
    }

    while (change>nickel) {
         count++;
         change = change -  nickel;
         break;
    }
    while (change>penny) {
        count++;
         change = change - penny;
         break;

    }
        printf ("%i\n", count);
}

1 Answer 1

0

There is no use of break here. it will make your loop to execute only once. break statement terminate the loop thats why youy are getting wrong output. and one more suggestion, convert change to dollor and use roundf function for correct answer

You must log in to answer this question.

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