CIS Logo SVC Logo

   Computing & Information Systems
   Department

 

Schoology Facebook        Search CIS Site      Tutorials

Software Design Using C++



Review of Loop Patterns



Answering the Question


Remember that we are trying to rewrite the test program so that the program now has the user interactively enter a list of items to buy. For each item to be purchased, the program should prompt the user to enter the unit price and the number of units of this item to buy. The program should output the cost for each type of item and, at the end, the total cost.

The following is the complete program that answers this question. Of course, you may have done things slightly differently. Note that the Results function was rewritten so as to only print the Cost, thus complying with the new problem description. Besides, it would not make a lot of sense to print the price and quantity right after the user just entered these.


/* Filename:  function.cpp

   Author:  Br. David Carlson

   Date:  December 21,2003

   This program finds the total cost for buying a list of items.  For input,
   the program prompts the user to enter the unit price and number to buy for
   each item in the list.  A unit price of zero (or a negative) is entered in order
   to end data entry.  Tax is not added, but the program does give a discount of $5
   if the cost for all units of any particular item is over $80.  For output,
   the program displays the cost for each item and at the end shows the total cost.
*/

#include <iostream>
using namespace std;


// Function prototypes:
float CalculateCost(float Price, int NumPurchased);
void Results(float Price, int NumPurchased, float & TotalCost);


int main(void)
   {
   float Price, TotalCost;
   int Num;

   TotalCost = 0.0;

   cout << "Enter the price for the current item to be purchased (or 0 to quit): ";
   cin >> Price;

   // To be exact, we should use != 0 here, but a negative price is not valid
   // so we can end the program on a negative price as well as zero.
   while (Price > 0)
      {
      cout << "Enter the number of units of this item to be purchased: ";
      cin >> Num;
      Results(Price, Num, TotalCost);
      cout << "Enter the price for the current item to be purchased (or 0 to quit): ";
      cin >> Price;
      }

   cout << "Total cost: " << TotalCost << endl;

   return 0;
   }

/* Given:  Price          The unit price of an item to be purchased.
           NumPurchased   The number of this item to be purchased.
           TotalCost      The total cost of all items purchased thus far.
   Task:   To find the cost of purchasing NumPurchased units of this item and
           to print this cost on the screen.  Also updates TotalCost to include
           the cost of purchasing these new items.
   Return: TotalCost      Updated total cost of all items purchased thus far.
*/
void Results(float Price, int NumPurchased, float & TotalCost)
   {
   float Cost;

   Cost = CalculateCost(Price, NumPurchased);
   TotalCost = TotalCost + Cost;
   cout << "Cost: " << Cost << endl << endl;
   }

/* Given:  Price          The unit price of an item to be purchased.
           NumPurchased   The number of this item to be purchased.
   Task:   To find the cost of purchasing NumPurchased items at unit price Price.
           A discount of $5 is given if the cost of the NumPurchased items is over $80.
   Return: This cost in the function name.
*/
float CalculateCost(float Price, int NumPurchased)
   {
   float TempCost;

   TempCost = Price * NumPurchased;
   if (TempCost > 80.0)
      TempCost = TempCost - 5.0;

   return TempCost;
   }

You can go back to the overall review listing: Review of Introductory Topics.

Back to the main page for Software Design Using C++

Author: Br. David Carlson with contributions by Br. Isidore Minerd
Last updated: January 15, 2013
Disclaimer