Sign in to follow this  

Disappearing Penny!!

This topic is 3738 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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"); }

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.)

Share this post


Link to post
Share on other sites

This topic is 3738 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this