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

int main(void)

{

float change;

int coins = 0;
do
{
    change  = get_float("Change owed: ");
}
while (change < 0);

int c = change * 100;
while (c >= 25)
{
    coins++;
    c = c - 25;
}

    while (c >= 10 && c < 25)
{
    coins++;
    c = c - 10;
}

    while (c >= 5 && c < 10)
{
    coins++;
    c = c - 5;
}

    while (c >= 1 && c < 5)
{
    coins = coins + 1;
    c--;    
}
printf("%i\n", coins);

}

when I enter 4.2, the output should be 18, but it is coming out to be 22. Can someone please tell what is wrong with my code? Thank you....

1

At this point, it's very important that you develop your debugging skills, so I'll point you in the right direction.

What is the value of c after it is created and initialized but before the first while loop is executed, and the input is $4.20???

Can you explain why?

5
  • It is equal to our input multiplied by 100. And then the value of c goes to the first loop. Jan 31 '20 at 10:10
  • @AbhigyaShridhar Set a breakpoint on that multiplication (by clicking on the line number in the editor) and run debug50 ./cash instead of ./cash. Watch the value of change and c as you step through the code. Alternatively, do some printf debugging, printing intermediate values to the terminal.
    – Blauelf
    Jan 31 '20 at 10:27
  • The output for 4.2 is coming out to be 419 instead of 420! I don't understand!!! Feb 2 '20 at 5:08
  • Well, that's the whole point of the exercise and the related lectures and lessons. WHY would it be 419 instead of 420? Is it possible that's been explained somewhere? THAT'S what you need to learn here! But I will give you a pointer. Try printing out the value of change after the while loop, but print it to at least 32 decimal places.
    – Cliff B
    Feb 2 '20 at 5:35
  • OH, I got that! I remember now that this slight inaccuracy was mentioned in the lecture. I will go through that again.. Thank you very much!! Feb 2 '20 at 5:48

You must log in to answer this question.

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