Assistance with Code Please

Hi Everyone

I am new to this coding and I am strugeling with a code that I am testing.
Can somebody please review it and advise me what the problem mights be.

Please review the following.

1
2
3
4
5
void updateDifference(float cashPriceP, float creditPriceP, float totalOfDifferencesP)
{
  totalOfDifferencesP = creditPriceP - cashPriceP;

}


why does totalOfDifferences give me a value of 0 when I know it is not 0?

cout << "The total of the differences between the cash and credit" << " prices is now R " << totalOfDifferences << endl;

I have inserted the whole code if anybody wants to see it.
Any assistance will be greatly appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//Test Code
#include <iostream>

using namespace std;
// The required function inputAndValidate should be inserted here.
void inputAndValidate(string & descriptionP, int & shelfStayP, float & wholesalePriceP)
{
	
    cin >> descriptionP >> shelfStayP;
    do
      {
        cin >> wholesalePriceP;
      }
      while (wholesalePriceP <= 0);
}
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;
}
void updateDifference(float cashPriceP, float creditPriceP, float totalOfDifferencesP)
{
  totalOfDifferencesP = creditPriceP - cashPriceP;

}
// The required function totalOfDifferences must be inserted here
int main( )
{
	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 " << totalOfDifferences << endl;
	return 0;

}
If I understand correctly, totalOfDifferencesP is output parameter for updateDifference, so it must be a reference:
void updateDifference(float cashPriceP, float creditPriceP, float& totalOfDifferencesP)

Regards
Genius, thanks it is working.
Topic archived. No new replies allowed.