0

I wrote this change calculator, and everything works fine except the case when I input $2.30 as value for changeAmount. How can I fix it? This the code:

#include <iostream>

using namespace std;

int main()
{
double changeAmount;
int fives;
int ones;
int quarters;
int dimes;
int nickels;
int pennies;
cout <<"*** A Very Simple Change Machine ***" <<endl;
cout <<endl;
cout <<"Enter change amount --> ";
cin >>changeAmount;
cout <<"The change amount is $" <<changeAmount <<" ." <<endl;
cout <<endl;

if (changeAmount < 0)
{
    cout << "Invalid amount." <<endl;
}
else
{

    changeAmount = changeAmount * 100;
    changeAmount = int(changeAmount);

    fives = changeAmount / 500 ;
    changeAmount -= fives * 500;

    ones = changeAmount / 100 ;
    changeAmount -= ones * 100;

    quarters = changeAmount / 25;
    changeAmount -= quarters * 25;

    dimes = changeAmount / 10;
    changeAmount -= dimes * 10;

    nickels = changeAmount / 5;
    changeAmount -= nickels * 5;

    pennies = changeAmount / 1 ;
    changeAmount -= pennies * 1;

    cout <<"Your change is given as :" <<endl;
    cout <<"    $5 bill (s)   : " <<fives <<endl;
    cout <<"    $1 bill (s)   : " <<ones <<endl;
    cout <<"    quarter (s)   : " <<quarters <<endl;
    cout <<"    dime (s)      : " <<dimes <<endl;
    cout <<"    nickel (s)    : " <<nickels <<endl;
    cout <<"    penny (ies)   : " <<pennies <<endl;
}
return 0;
}
Adam Rich
  • 86
  • 2
  • 10
  • First, you should step through your code with a debugger to find out where the problem is. – Code-Apprentice Sep 27 '14 at 21:40
  • Start by using the debugger. As a note, your `pennies = changeAmount / 1;` is the same as `pennies = changeAmount;`, and you can remove the `changeAmount -= pennies * 1;` entirely because it's meaningless. (You don't refer to `changeAmount` any further after that at all, so there's no need to update the value, and if you needed to you could use `changeAmount = 0;` to avoid the unnecessary multiplication and subtraction.) – Ken White Sep 27 '14 at 21:46
  • 3
    floating-point for financial calculations. Ouch, that hurts! – Deduplicator Sep 27 '14 at 21:47

3 Answers3

1

The problem is that you are doing modulo arithmetics using a double. This does not work reliably. You can change the type of changeAmount to int to make the example work. (You might want to introduce an second varialbe that stores the double read from the input.)

SebastianK
  • 3,582
  • 3
  • 30
  • 48
0

compile this and see what happens:

changeAmount = changeAmount * 100;
cout <<"The change amount is $" <<changeAmount <<" ." <<endl; //230
changeAmount = int(changeAmount);
cout <<"The change amount is $" <<changeAmount <<" ." <<endl; //229
atlanteh
  • 5,615
  • 2
  • 33
  • 54
-1

The issue is with how decimals are stored in the system. 2.30 is stored as 2.29.... and when you multiply by 100 and take the integer portion you get 229. When multiplying by 100 make sure to round up using the round function. Make sure to include Math library.

else
{

    changeAmount = round(changeAmount * 100);
    changeAmount = int(changeAmount);

}
  • actually I just noticed that when I start debugging. I don't know how to use a round function but I thought about adding 0.0000000001 to `changeAmount` after multiplying by 100. – Adam Rich Sep 27 '14 at 22:15
  • Just #include in the header, and then use the code I posted above. – Emmett Wilson Sep 27 '14 at 22:20
  • I had to use `ceil` function because round didnt work. but it worked. thanks :) – Adam Rich Sep 27 '14 at 23:36
  • The ciel function always rounds up, thus will cause problems when the system stores a decimal slightly larger than what you want. For example 1.1 is stored as 1.1000000000000001 if you use ciel(100 * 1.1) you will get 111 instead of 110 thus you want to round to the nearest integer using changeAmount = round(changeAmount * 100); – Emmett Wilson Sep 28 '14 at 00:17
  • its telling me the function `round` is undefined. – Adam Rich Sep 28 '14 at 04:49
  • are you using a double or a float for change amount now? If you use a double round(changeamount*100); will work, if you use a float use roundf(changeamount*100); I changed 2 lines of your code as you posted and it worked. #include at the top and the line changeamounbt = round(changeamount*100); and it worked just fine. – Emmett Wilson Sep 28 '14 at 12:16