/* This is a driver program to test function MakeChange().

   Input:  The cost of an item, and the amount paid
   Output: The change in terms of numbers of dollars, half-dollars,
           quarters, dimes, nickels, and pennies.
------------------------------------------------------------------*/

#include <iostream.h>

void MakeChange(double, double, int&, int&, int&, int&, int&, int&);

int main(void)
{
   const char
      Sentinel[] = "a negative value ";

   cout << "\nThis program tests a change-making function...\n";

   double
      ItemCost,          // a purchase
      AmountPaid;        // what was paid
   int                   // the amount of change
      NumDollars,
      NumHalfDollars,
      NumQuarters,
      NumDimes,
      NumNickels,
      NumPennies;

   cout << "\nEnter item cost, and amount paid ("
        << Sentinel << "to quit): ";
   cin >> ItemCost;

   while (ItemCost >= 0)
   {
      cin >> AmountPaid;

      MakeChange(ItemCost, AmountPaid, NumDollars, NumHalfDollars,
                 NumQuarters, NumDimes, NumNickels, NumPennies);

      cout << "\nThe change from this purchase is:\n"
           << NumDollars << " dollars,\n"
           << NumHalfDollars << " half-dollars,\n"
           << NumQuarters << " quarters,\n"
           << NumDimes << " dimes,\n"
           << NumNickels << " nickels, and\n"
           << NumPennies << " pennies.\n";
      cout << "\nEnter item cost, and amount paid ("
           << Sentinel << "to quit): ";
      cin >> ItemCost;
   }

   return 0;
}

/* This function computes the dollars, half-dollars, etc. of
   change given the amount of a purchase and the payment made.

   Receive: PurchaseAmount, the (real) amount of the purchase
            Payment, the (real) amount of the payment

   Return:  Dollars, the (integer) number of dollars in change
            HalfDollars, the (integer) number of half-dollars in
               change
            Quarters, the (integer) number of quarters in change
            Dimes, the (integer) number of dimes in change
            Nickels, the (integer) number of nickels in change
            Pennies, the (integer) number of pennies in change
------------------------------------------------------------------*/

void MakeChange(double PurchaseAmount, // the amount of the purchase
                double Payment,        // the amount of the payment
                int& Dollars,          // dollars of change
                int& HalfDollars,      // half-dollars of change
                int& Quarters,         // quarters of change
                int& Dimes,            // dimes of change
                int& Nickels,          // nickels of change
                int& Pennies )         // pennies of change
{
   int                                 // total change in pennies
      Change = int(100.0 * (Payment - PurchaseAmount) + 0.5);

   if (Change > 0)
   {
      Dollars = Change / 100;       // 100 pennies per dollar
      Change %= 100;                // compute remaining change

      HalfDollars = Change / 50;    // 50 pennies per half dollar
      Change %= 50;                 // compute remaining change

      Quarters = Change / 25;       // 25 pennies per quarter
      Change %= 25;                 // compute remaining change

      Dimes = Change / 10;          // 10 pennies per dime
      Change %= 10;                 // compute remaining change

      Nickels = Change / 5;         // 5 pennies per nickel
      Pennies = Change % 5;         // pennies are all that's left
   }
   else
      Dollars = HalfDollars = Quarters
              = Dimes = Nickels = Pennies = 0;
}

