0

I just try to run this simply code but wtihout success. Seems that there is no exit from the loop or even getFloat() is not working as printf inside do while loop is not executed.

https://gist.github.com/anonymous/4c129645e6b465ae3a0a

5
  • I just tested it, and works as expected accordingly to the code. I am not sure what is your problem. printf() is executed for me
    – wallek876
    Jan 7 '15 at 12:47
  • take a look at the output in terminal imgur.com/fgqLm8d Jan 7 '15 at 12:50
  • This is the output: jharvard@ubuntu (~/test): ./1 0.000000How much money you want to get back? 5.25 5.2500005.250000
    – wallek876
    Jan 7 '15 at 12:51
  • yeap, it should be as you already show here. Take a look at my output: imgur.com/fgqLm8d . I've tried to use make, clang, restart VM, etc... no clue what's goin on here Jan 7 '15 at 12:59
  • Well, the code in the link on the question is not the same that the one in the image you link in the comment. You should edit the post to update the question and then i will post an answer accordingly. Your problem is that you are in an infinite loop.
    – wallek876
    Jan 7 '15 at 13:14
0

Yor problem is that your do while loop:

do
{
counter25 = f - 0.25;
} while(counter25 >0);

it never ends because f is the value introduced by the user, lets say 2, and this value never changes. So f - 0.25 is always 1.75 in every iteration of the loop.

And note that counter25 is an int and when you assign a floating point value to an int it truncates the decimals, and counter25 in this case becomes 1 which is greater than 0, This is done in every iteration, and is why your loop never ends, because counter25 is always greater than 0.

So the program never arrives to the last printf().

You must log in to answer this question.

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