469,575 Members | 1,628 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,575 developers. It's quick & easy.

Multiplying money by 100 - loss of pennies

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
4 1729
weaknessforcats
9,208 Expert Mod 8TB
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
ejack
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
9,208 Expert Mod 8TB
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
ejack
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.

Similar topics

24 posts views Thread by Batista, Facundo | last post: by
12 posts views Thread by David Garamond | last post: by
8 posts views Thread by John | last post: by
45 posts views Thread by Master Programmer | last post: by
9 posts views Thread by bb | last post: by
KoreyAusTex
7 posts views Thread by KoreyAusTex | last post: by
1 post views Thread by =?Utf-8?B?dHRocm9uZQ==?= | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.