0

http://paste.ubuntu.com/7827955/

At this stage, I want the program to get a dollar amount from the user, convert it to cents, and then (just for now to confirm it's been done right). Based on what I understand about round(), this should be working. However, when I run it and input, say, 2.00, the output is not 200 but 0x1.49e0000000002p-895. What am I doing wrong?

3

No need to have a condition before the loop since the loop itself has a continuation condition

// the loop is enough
while (owed < 0)
{
    // the code here is executed while owed < 0
}

Use %d or %i to print ints not %a

printf("%d\n", amountInCents);
2

%a in printf() is used to print a float as a hexidecimal. That is why you are getting the 0x1.49e000... number in the output. Use %i or %d for an integer.

And as was pointed out, you don't need to preceed the while(condition) with an if(condition) with both containing the same condition, as the while block is only entered if the condition is true anyway.

1
  • That's true. Hexadecimal is very different to integers.
    – ChiCubed
    Jul 21 '14 at 4:11

You must log in to answer this question.

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