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;
}