Disappearing Penny!!

Started by
6 comments, last by Zahlman 16 years, 7 months ago
Hey folks hope the subject caught your eye. I'm in an intro to programming class at my local junior college and on of the assignments was to find out the cost of some items at a store, how much the customer paid, and then what there change would be in dollars, quarters, dimes, etc... I didn't have any trouble setting the program up to run, but when I did run it, I would sometimes be short a penny at the end - not always, but sometimes. I know this doesn't actually have anything to do with a game I might be writing, but I'm trying to build some fundamentals towards that end. So, if anyone can tell how to fix the problem, and then if maybe you could tell me why the problem was there, I would give you a huge e-hug for it. Thanks all. #include <iostream> #include <cstdlib> using namespace std; main () { double cost; double paid; double change; int changeInt; int dollars; int quarters; int dimes; int nickels; int pennies; cout<<"Please enter the total amount owed"<<endl; cin>>cost; cout<<"\nAnd now how much is being paid"<<endl; cin>>paid; change=paid-cost; cout<<"Your change is "<<change<<" which will be paid out as...\n"; changeInt=change*100; dollars=changeInt/100; cout<<dollars<<" dollar(s)"<<endl; changeInt=changeInt-dollars*100; quarters=changeInt/25; cout<<quarters<<" quarter(s)"<<endl; changeInt=changeInt-quarters*25; dimes=changeInt/10; cout<<dimes<<" dime(s)"<<endl; changeInt=changeInt-dimes*10; nickels=changeInt/5; cout<<nickels<<" nickel(s)"<<endl; changeInt=changeInt-nickels*5; pennies=changeInt; cout<<"and "<<pennies<<" pennies.\n\n\n"; system ("pause"); }
Advertisement
The problem is that you're using a floating point variable to represent the original cost. Due to floating point inaccuracy, something like $2.73 may actually be represented internally as 2.72999999953212. (Here's why.) To get around this, you should make sure to round to the nearest cent as opposed to rounding down (as implicitly happens when you assign a floating point number to an integer variable). The easiest way to do this, for positive numbers anyway, is simply to add 0.5 to the amount of cents before assigning it to the int.
I love you major e-hug!! I actually lost sleep last night wondering what happened to that dang penny. Thanks a ton! :-D
If you're using Microsoft Visual C++, it'd probably be best for you to read this article, which should teach a lot of what you need to know to track down and fix the problem yourself next time.

Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse

Additionally with constrained numbers like this project (0.01 being the smallest value) you might want to consider representing the floating point value in an integer.

(pardon formatting, its been a while since being on the forums and don't want to look up how to format tables at the moment)

integer value | real value
1 | 0.01
5 | 0.05
10 | 0.10
25 | 0.25
100 | 1.00

So you would multiply the inputs by 100 to get the represented value.
- My $0.02
Double quote?! I never double quote!

... sorry.
- My $0.02
Quote:Original post by Drethon
- My $0.02
I think you mean "My $0.01999999723".
Quote:Original post by Drethon
Additionally with constrained numbers like this project (0.01 being the smallest value) you might want to consider representing the floating point value in an integer.

(pardon formatting, its been a while since being on the forums and don't want to look up how to format tables at the moment)

integer value | real value
1 | 0.01
5 | 0.05
10 | 0.10
25 | 0.25
100 | 1.00

So you would multiply the inputs by 100 to get the represented value.


Or, expressed more simply, track the number of cents instead of the number of dollars. :)

(But this still leaves the problem of reading and interpreting the input.)

This topic is closed to new replies.

Advertisement