By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,475 Members | 1,075 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,475 IT Pros & Developers. It's quick & easy.

Multiplying money by 100 - loss of pennies

P: 12
Hello! I am having issues with my code. My program is supposed to take a given amount of money (1.33) and tell how many quarters, dimes, nickels and pennies would make up this sum of money. Problem is when I multiply the input by 100, depending on the amount of money entered, I lose a penny. When I enter .34 it comes out correct, but when I enter .35, it gives me 34. Any and all help is greatly appreciated. Here is part of my code:
Expand|Select|Wrap|Line Numbers
  1.     void ChangeMaker::Calc()
  2.     {
  3.         int q = 25;
  4.         int d = 10;
  5.         int n = 05;
  6.         int p = 01;
  7.         int remainderQ = 0;
  8.         int remainderD = 0;
  9.         int remainderN = 0;
  10.  
  11.  
  12.         int newAmount = static_cast<int>(amount * 100);
  13.         cout << newAmount << endl;
  14.         if(newAmount > .24)
  15.         {
  16.             quarters = (newAmount/q);
  17.             remainderQ = (newAmount % q);
  18.         }
  19.         if(remainderQ > .09)
  20.         {
  21.             dimes = (remainderQ/d);
  22.             remainderD = (remainderQ % d);
  23.         }
  24.         if(remainderD > .04)
  25.         {
  26.             nickels = (remainderD/n);
  27.             remainderN = (remainderD % n);
  28.         }
  29.         if(remainderN > .00)
  30.         {
  31.             pennies = remainderN;
  32.         }
  33.     }
  34.  
Mar 26 '08 #1
Share this Question
Share on Google+
4 Replies

weaknessforcats
Expert Mod 5K+
P: 9,197
int newAmount = static_cast<int>(amount * 100);
cout << newAmount << endl;
if(newAmount > .24)
Err....newAmount is an int. Ints do not have decimal places.

You mean:
[code=cpp
if (newAmount > 24)
etc...
[/code]

And take that cast out of there.

Do everything in pennies.

Certainly you can write a function with an int argument that can display an int value of 123 as $1.23.

You do not need decimals in a fincance application. They are just eye candy.
Mar 26 '08 #2

P: 12
"amount" is a double. When the user enters an amount, they can enter 5.23. Then what I'm trying to do is take that 5.23 and multiply by 100 so I can work with the modulus operator. Can you show me a better way? Thank you!

Err....newAmount is an int. Ints do not have decimal places.

You mean:
[code=cpp
if (newAmount > 24)
etc...
[/code]

And take that cast out of there.

Do everything in pennies.

Certainly you can write a function with an int argument that can display an int value of 123 as $1.23.

You do not need decimals in a fincance application. They are just eye candy.
Mar 26 '08 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
Your amount should be int.

Let's say the user enters 5.24.

You parse the input to the decimal point, pick out the numbers to the left of the decimal, convert them to an int, multiply by 100, put that in the amount then and add the two digits after the decimal to get 524.

The code in main() would look like:
Expand|Select|Wrap|Line Numbers
  1. char buffer[80];
  2. cin.getline(buffer,80);
  3. int amount = Convert(buffer);
  4.  
and off you go.

Floating point is intended for scientific use where accuracy is not required. The automatic rounding of floating point can throw off a financial application.
Mar 26 '08 #4

P: 12
Wow! Forgive me for being so uneducated, but that went way over my head. I think I get the big picture though. I've changed everything to ints and it seems to be working properly. Thank you for your help.
Mar 26 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.