# Thread: help! I lost 1 cent_C++ beginning

1. ## help! I lost 1 cent_C++ beginning

I need to write a program that interacts between cash register and machine. As she inputs total price and payment, the machine will calculate money return in dollars, quaters, dimes, nickles and cents.
After playing with coding, I regconize I lose 1 cent in some situation. I think converting from "int" -->"double" then return back to "int" in my sourcecode cause this problem, but I can't find the way to fix. Please check my sourcecode and provide me a good solution.
Code:
```#include <iostream.h>
#include "textlib.h"
#include <iomanip.h>

int main()
{

//Variable declaration
double payment, totalprice, exchange;
int dollars, quarters, dimes, nickles, pennies, coinChange;

// Input from cash register
cout << "Enter total price and your payment: ";
cin >> totalprice >> payment;

//Calucate the exchange
exchange = payment - totalprice;

//Determine the money return in dollars, quaters, nickels and pennies
dollars = int(exchange);

coinChange =int((exchange - dollars) *100);

quarters = coinChange / 25;
coinChange %= 25;

dimes = coinChange / 10;
coinChange %= 10;

nickles = coinChange / 5;

pennies = coinChange % 5;

//Output detail transaction

cout << "Purchase total:"<< setreal (20,2) << totalprice << endl;
cout << "Payment       :"<< setreal (20,2) << payment << endl;
cout << "Change        :"<< setreal (20,2) << exchange << endl;
cout << "Dollars       :"<< setw(20) << dollars << endl;
cout << "Quarters      :"<< setw(20) << quarters << endl;
cout << "Dimes         :"<< setw(20) << dimes << endl;
cout << "Nickles       :"<< setw(20) << nickles << endl;
cout << "Pennies       :"<< setw(20) << pennies << endl;

return 0;
}```

2. Other than not being familiar with the setreal manipulator you use to format the output, I don't see a problem with your code. The way I'd try to track down the problem is to insert cout statements after each value is determined in addition to at the end (ya, I know it's overkill, but sometimes I'll pick up something that way). Then I'd run a number of trials keeping track of expected output and observed output looking for a pattern. Then I'd evaluate the cause for the pattern. Maybe it's a rounding error someplace, maybe it's a bug in your compiler. If you can narrow it down, that might help someone help you figure it out.

3. Other than not being familiar with the setreal manipulator you use to format the output, I don't see a problem with your code
setreal(), which takes 2 arguments "w" (width) and "p" (position), works similarly to setw() but much more power by defining how many significants after ".". For example, the input is real number 8.3243242, then setreal(20,2) will output 8.32 and setw(20).
I tracked down the value "coinChange" like yours. As I said "I think converting from "int" -->"double" then return back to "int" in my sourcecode cause this problem", precisely at this line
Code:
`coinChange =int((exchange - dollars) *100);`
I lost 1 cent.

Note: I tested with 2 possible ways in Visual C++ 5.0 and 6.0. I enterd total price and payment 3.08 and 10. The program works fine! but with 3.08 and 9.99 I lost 1 cent.

4. Floating point numbers can't represent all whole numbers precisely. One possible solution is to add .5 to your calculation before rounding:
Code:
`	coinChange =int((exchange - dollars) *100 + 0.5);`
Or another idea is instead of using doubles, read in the amounts as strings, then separate each into dollars and cents and store them as ints.

5. I remember this problem... yeah, using floats will lose you a penny here and there... it shouldn't happen all the time though... if your constantly over/under a penny, then something's wrong, but if it's just intermittent, don't worry about your code... this is how I would solve it:
Code:
```int Amount;
float Currency;

//take in currency
std::cin>>Currency;

//to help float innacuracy
amount=static_cast<int>(Currency*100);

//do math/other stuff here (don't forget your working in pennies now)

//then for output
currency=static_cast<float>(currency)/100.0;```

6. IEEE floats are (basically) constructed by summing up a series of up to 23 times 1/2^n. If 1/2^n fits into the number, bit number n is set. As you can see, numbers like 0.50, 0.75, 0.25 ... have an exact binnary representation (10000...(18 more "0") and 11000...(18 more...) and 01000 (18 you figure it...)), but a lot of other numbers don't have it (like an ordinary 0.8).

I'd suggest you read in a string and strip the dot before converting it into integer. That way you don't have to work with floats at all. Of course you are working with a value 100 times that of the user input, so you'd have to take care of that in your calculations. You will also need to make sure that the user won't enter something like 123.456 (3 numbers after dot)

7. I agree, store it as an integer number of pennies and use integer math, it makes the most sense.