Public Group

# Disappearing Penny!!

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

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

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 I love you major e-hug!! I actually lost sleep last night wondering what happened to that dang penny. Thanks a ton! :-D #### Share this post ##### Link to post ##### Share on other sites 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. #### 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 Double quote?! I never double quote! ... sorry. #### Share this post ##### Link to post ##### Share on other sites Quote:  Original post by Drethon- My$0.02
I think you mean "My \$0.01999999723".

##### Share on other sites
Quote:
 Original post by DrethonAdditionally 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 value1 | 0.015 | 0.0510 | 0.1025 | 0.25100 | 1.00So 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.)

1. 1
2. 2
Rutin
18
3. 3
khawk
15
4. 4
A4L
14
5. 5

• 10
• 13
• 26
• 10
• 11
• ### Forum Statistics

• Total Topics
633745
• Total Posts
3013667
×