/* changemaker5.cpp is a driver program that tests function MakeChange().
 *
 * Input:  the cost of an item, and the amount paid
 * Output: the change in terms of numbers of dollars, quarters, 
 *         dimes, nickels, and pennies
 ***********************************************************************/

#include <iostream>                // cout, cin, <<, >>
#include <string>                  // string

void MakeChange(double purchaseAmount, double payment,
                int & dollars, int & quarters, int & dimes,
                int & nickels, int & pennies);

int main()
{
   const string SENTINEL = "a negative value";

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

   double itemCost,          // a purchase
          amountPaid;        // what was paid

   int numDollars,           // variables for
       numQuarters,          //  the values
       numDimes,             //  to be output
       numNickels,
       numPennies;


   for (;;)
   {
      cout << "Enter item cost and amount paid ("
           << SENTINEL << " to quit): ";
      cin >> itemCost;

      if (itemCost < 0) break;

      cin >> amountPaid;

      MakeChange(itemCost, amountPaid, numDollars,
                 numQuarters, numDimes, numNickels, numPennies);

      cout << "The change from this purchase is:\n"
           << numDollars << " dollars,\n"
           << numQuarters << " quarters,\n"
           << numDimes << " dimes,\n"
           << numNickels << " nickels, and\n"
           << numPennies << " pennies\n\n";
   }
   return 0;
}

/* MakeChange computes the dollars, quarters, dimes, nickels, and
 * pennies in change given the amount of a purchase and the amount paid.
 *
 * Receive:      purchaseAmount, the (real) amount of the purchase,
 *               payment, the (real) amount of the payment
 * Precondition: purchaseAmount <= payment.
 * Pass back:    dollars, the (integer) number of dollars, 
 *               quarters, the (integer) number of quarters,
 *               dimes, the (integer) number of dimes,  
 *               nickels, the (integer) number of nickels, and
 *               pennies, the (integer) number of pennies in change
 **********************************************************************/

void MakeChange(double purchaseAmount,  // amount of purchase
	             double payment,         // amount of payment
	             int & dollars,          // dollars of change
	             int & quarters,         // quarters of change
	             int & dimes,            // dimes of change
	             int & nickels,          // nickels of change
	             int & pennies)          // pennies of change
{
   int change = int(100.0 * (payment - purchaseAmount) + 0.5);

   if (change > 0)
   {
      dollars = change / 100;       // 100 pennies per dollar
      change %= 100;                // 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
   {
      cerr << "*** Purchase amount: " << purchaseAmount
           << " exceeds payment: " << payment << endl;
      dollars = quarters = dimes = nickels = pennies = 0;
   }


