/* Filename: purchase.cpp Author: Br. David Carlson Date: June 16, 1998 Revised: Jul 31, 2000; Nov 21, 2001, Jan 24, 2013. This program computes the total cost for each of a series of purchases and then presents the grand total cost for all of the purchases. The user is prompted to enter the quantity bought and unit price for each purchase. For each purchase, the total cost is printed and the user is then asked whether or not to do another purchase. When the user chooses not to do further purchases, the grand total cost is printed. Tested with: Microsoft Visual C++ 6.0 Microsoft Visual C++ .NET g++ under Linux */ #include using namespace std; void OnePurchase(float & GrandTotal); int main(void) { float GrandTotal = 0.0; char Reply; do { OnePurchase(GrandTotal); cout << "Do another purchase (y/n)? "; cin >> Reply; } while ((Reply == 'Y') || (Reply == 'y')); cout << endl << "Grand total cost: " << GrandTotal << endl; return 0; } /* Given: GrandTotal The total cost of all purchases made thus far. Task: To prompt the user for the info needed to make one purchase, printing the cost of the purchase on the screen, and updating GrandTotal. Return: GrandTotal The updated total cost of all purchases made. */ void OnePurchase(float & GrandTotal) { int Quantity; float Price, Total; cout << endl << "Enter the quantity to be purchased: "; cin >> Quantity; cout << "Enter the unit price: "; cin >> Price; Total = Quantity * Price; GrandTotal = GrandTotal + Total; cout << endl << "Total cost: " << Total << endl << endl; }