CIS Logo SVC Logo

   Computing & Information Systems
   Department

 

Schoology Facebook        Search CIS Site      Tutorials

Software Design Using C++



Review of Loop Patterns



Question


Rewrite the test program below, which finds the costs for buying 2 groups of items, so that the program instead 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. If you need to review loop patterns first, go to the Programming Patterns section of these web pages.


/* Filename:  function.cpp

   Author:  Br. David Carlson

   Date:  December 21,2003

   This is a test program that totals the cost for buying a list of items.
   The price and number of units to buy for each item are hard-coded in this
   test program.  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.  The price,
   number of units, and costs for each item and the total cost are printed on
   the screen.
*/

#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;

   Price = 3.89;
   Num = 2;
   Results(Price, Num, TotalCost);

   Price = 30.21;
   Num = 3;
   Results(Price, Num, TotalCost);

   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, together with Price and NumPurchased.
           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 << "Unit price: " << Price << "   Number purchased: "
      << NumPurchased << "   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;
   }

Click on NEXT to see hints and possible answers. A new browser window will be used to display the new page so that you can continue to refer to the above code in the old browser window.

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