0

I am wandering around for answers. But I end up that my code is correct ( I'm 90% sure :P )

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


int main(void)
{
    double change;
    do
    {
        printf("What do i owe you ?\n");
        change = get_double();
    }
    while (change < 0 );
    int times=0;

    while (change > 0.24){
            change = change - 0.25;
            times++;
        }
    while (change > 0.09){
            change = change - 0.10;
            times++;
        }
    while (change > 0.04){
            change = change - 0.05;
            times++;
        }
    while (change > 0.0){
            change = change - 0.01;
            times++;
       }
    printf("%d\n" , times);
}

That's my code, but when checking i keep on taking wrong values when asking for 1.6 and 4.2 .

1 Answer 1

0

You are committing a round error when working with decimals, I think that per spec you have to change the coin to cents (multiplying by a hundred) and then dump that value into an integer

int c = round (change * 100);

So now you should use 25 instead of 0.25 etc

You must log in to answer this question.

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