Multiplying money by 100 - loss of pennies

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ejack
    New Member
    • Mar 2008
    • 12

    Multiplying money by 100 - loss of pennies

    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:
    Code:
    	void ChangeMaker::Calc()
    	{
    		int q = 25;
    		int d = 10;
    		int n = 05;
    		int p = 01;
    		int remainderQ = 0;
    		int remainderD = 0;
    		int remainderN = 0;
    
    		
    		int newAmount = static_cast<int>(amount * 100);
    		cout << newAmount << endl;
    		if(newAmount > .24)
    		{
    			quarters = (newAmount/q);
    			remainderQ = (newAmount % q);
    		}
    		if(remainderQ > .09)
    		{
    			dimes = (remainderQ/d);
    			remainderD = (remainderQ % d);
    		}
    		if(remainderD > .04)
    		{
    			nickels = (remainderD/n);
    			remainderN = (remainderD % n);
    		}
    		if(remainderN > .00)
    		{
    			pennies = remainderN;
    		}
    	}
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Originally posted by ejack
    int newAmount = static_cast<int >(amount * 100);
    cout << newAmount << endl;
    if(newAmount > .24)
    Err....newAmoun t 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.

    Comment

    • ejack
      New Member
      • Mar 2008
      • 12

      #3
      "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!

      Originally posted by weaknessforcats
      Err....newAmoun t 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.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        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:
        [code=c]
        char buffer[80];
        cin.getline(buf fer,80);
        int amount = Convert(buffer) ;
        [/code]

        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.

        Comment

        • ejack
          New Member
          • Mar 2008
          • 12

          #5
          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.

          Comment

          Working...