/* ------------------------------------------------------------------------
Coin machine.
Input price and payment.
Output change in terms of dollars, quarters, dimes, nickels and pennies.
------------------------------------------------------------------------ */
# include
using namespace std;
int main( )
{
double price;
double payment;
int changeInCents;
int dollars;
int quarters;
int dimes;
int nickels;
int pennies;
// enter price > 0
cout << "------------------------------------" << endl;
cout << "Enter the purchase price: ";
cin >> price;
while (price < 0)
{
cout << "The price must be positive." << endl;
cout << "Re-enter the purchase price: ";
cin >> price;
}
// enter payment > price
cout << "------------------------------------" << endl;
cout << "Enter the payment: ";
cin >> payment;
while (payment < price)
{
cout << "The payment must be greater than or equal to the price." << endl;
cout << "Re-enter the payment: ";
cin >> payment;
}
// compute change
changeInCents = static_cast(100.00*(payment - price)+0.05);
dollars = changeInCents/100;
changeInCents = changeInCents - dollars*100;
quarters = changeInCents/25;
changeInCents = changeInCents - quarters*25;
dimes = changeInCents/10;
changeInCents = changeInCents - dimes*10;
nickels = changeInCents/5;
changeInCents = changeInCents - nickels*5;
pennies = changeInCents;
// output change if any
cout << "------------------------------------" << endl;
cout << "The correct change is." << endl;
if (dollars + quarters + dimes + nickels + pennies == 0)
cout << "No change." << endl;
if (dollars > 0)
{
if (dollars == 1)
cout << "1 dollar" << endl;
else
cout << dollars << " dollars" << endl;
}
if (quarters > 0)
{
if (quarters == 1)
cout << "1 quarter" << endl;
else
cout << quarters << " quarters" << endl;
}
if (dimes > 0)
{
if (dimes == 1)
cout << "1 dime" << endl;
else
cout << dimes << " dimes" << endl;
}
if (nickels > 0)
{
if (nickels == 1)
cout << "1 nickel" << endl;
else
cout << nickels << " nickels" << endl;
}
if (pennies > 0)
{
if (pennies == 1)
cout << "1 penny" << endl;
else
cout << pennies << " pennies" << endl;
}
cout << "------------------------------------" << endl;
return 0;
}
               (
geocities.com/vuumanj)