The CashAndCreditShop supermarket determines the retail price of a product based on the time it is expected to stay
on the shelf. Any item that is expected to sell in one week or less is marked up 10%, and any item that is expected to
stay on the shelf for more than one week is marked up 15% over the wholesale price. The low markup price is used
for all items that are expected to stay on the shelf for up to 7 days and for all items that stays longer than 7 days on
the shelf the higher markup is used.
Furthermore, all items are marked with two prices – one for paying cash, and one for buying on credit. Items bought
on credit have a further markup of 2% on the initial markup.
Write a C++ program to help CashAndCreditShop to determine the retail price for the items in their shop, based on
the wholesale price and the number of days an item is expected to stay on the shelf.
The program is developed in three steps. You have to submit printouts for Question 5c only.
Question 5a: Three reference parameters
Write a void function inputAndValidate where the description and the wholesale price for an item as well as
the number of days it is expected to stay on the shelf are input and validated. The function should have three
reference parameters, namely a product description (a string value), a parameter that stores the wholesale price
(of type float) and a parameter that stores the number of days it is expected to stay on the shelf (of type int).
The third parameter has to be validated by using a do..while loop: the program should keep on displaying a
prompting message until the user enters a value bigger than 0.
We give the main function below. Test your program to make sure that it works correctly, but do not submit any
printouts.

// Assignment 2 Question 5a
#include <iostream>
using namespace std;
// The required function inputAndValidate should be inserted here.

int main( )
{
string description;
int shelfStay;
float wholesalePrice;
inputAndValidate(description, shelfStay, wholesalePrice);
cout.setf(ios::fixed);
cout.precision(2);
cout << description << " is expected to stay on the shelf for "
<< shelfStay <<" days and has a wholesale price of R"
<< wholesalePrice << endl;
return 0;
}

Question 5b: Value parameters and reference parameters
In order to calculate the cash price and the credit price we need to know the wholesale price for an item and the
number of days it is expected to stay on the shelf.
We give the code for the void function determinePrices to calculate the cash price and the credit price
below. This function has two value parameters that supply the wholesale price for an item and the number of days it
is expected to stay on the shelf and two reference parameters, namely the cash price and the credit price.
The CashAndCreditShop also wants to know the average price difference between the cash and credit prices. Write a
function updateDifference to determine the difference between the cash price and the credit price for an item
and to add this difference to the total of the differences. Function updateDifference has three parameters: two
value parameters namely the cash price and the credit price, and one reference parameter to hold the total of the
differences.
We give the main function and function determinePrices below. Test your program but do not submit any
printouts.

// Assignment 2 Question 5b
#include <iostream>
using namespace std;
// The required functions inputAndValidate must be inserted here
void determinePrices(int shelfStayP, float wholesalePriceP,
float & cashPriceP, float & creditPriceP)
{
if (shelfStayP <= 7)
cashPriceP = 1.1 * wholesalePriceP;
else cashPriceP = 1.15 * wholesalePriceP;
creditPriceP = 1.02 * cashPriceP;
}
// The required function totalOfDifferences must be inserted here
int main( )
54
{
string description;
int shelfStay;
float wholesalePrice, cashPrice, creditPrice, totalOfDifferences;
// initialise total
totalOfDifferences = 0;
inputAndValidate(description, shelfStay, wholesalePrice);
determinePrices(shelfStay, wholesalePrice, cashPrice,
creditPrice);
updateDifference (cashPrice, creditPrice, totalOfDifferences);
cout.setf(ios::fixed);
cout.precision(2);
cout << description << " is expected to stay on the shelf for "
<< shelfStay <<" days and has a wholesale price of R"
<< wholesalePrice << endl;
cout << " The cash price for " << description << " is R"
<< cashPrice << " and the wholesale price is R"
<< wholesalePrice << endl;
cout << "The total of the differences between the cash and credit"
<< " prices is now R " << endl;
return 0;
}

Question 5c: Final version
Declare a global constant NR_ITEMS and assign the value 6 to it. Now change the main function of Question 5b to
contain a for loop going from 1 to NR_ITEMS. The three functions in Questions 5a and 5b should be called inside
this loop. When the loop is exited, the average price difference between the cash and credit prices should be
calculated and displayed.
Run your program on the data below. Submit printouts of the program and output.
Butter 5 14.99
Milk 3 16.99
Bread 2 6.75
Salami 12 32.88
Wine 39 55.49
Newspaper 1 5.99

Edited 5 Years Ago by vavishy: n/a

Take it in small chunks. First, concentrate on question 5a. You are given the code that calls the function "inputAndValidate". The assignment calls for you to write this function.

The function needs to take in three parameters. The assignment says that these should be passed in as reference parameters. The reason for this is that if you pass them by value, when you get inside the "inputAndValidate" function and assign values to the variables, when you leave, the values you just assigned will "go away". The point of this part of the assignment is to teach you how to pass variables into a function so that you can assign values to them and get them back.

See this article on passing variables by reference: http://www.learncpp.com/cpp-tutorial/73-passing-arguments-by-reference/

Just work on getting that part of the assignment done, then, you can tackle the next part. The sample program that was given will print out the values of the variables once you get back from the function.

This article has been dead for over six months. Start a new discussion instead.