Display Commissions

Hey, everyone.
Essentially what I am trying to do is have the user input a sales amount, calculate the commission (10%), display the commission, and then allow the user to enter another sales amount that would then output the commission, and add the two (or more) commissions together to find the total commission. I have everything else done and I am close but I am having trouble with my commission not changing when I enter a new sales amount. I also have to use 4 functions, 1 value-returning and the other 3 void, which is the reason why I am doing it this way. Here is my code:

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <iomanip>
using namespace std;

//function prototypes
double totalSales(char totalChar, double &salesT);
void calcCom(double baseCom, double com, double &salesT);
void displayCom(double totalC, double com, double &salesT);
void displayTotCom(char totalChar, double totalC, double com, double &salesT);

int main() {

	const double baseCom = 0.1;
	double commission = 0.0;
	double totalCom = 0.0;
	double sales = 0.0;
	char total = ' ';

	totalSales(total, sales);

	while (sales >= 0) {
		if (sales >= 0) {
			calcCom(baseCom, commission, sales);
			commission = baseCom * sales;
			displayCom(totalCom, commission, sales);
			for (totalCom = 0.0; totalCom >= 0.0; totalCom += commission) {
				displayTotCom(total, commission, totalCom, sales);
				totalSales(total, sales);
			}
		}
		

		totalSales(total, sales);
	}



	return 0;
}

//function definitions
double totalSales(char totalChar, double &salesT)
{
	cout << "What is the total sales amount? ";
	cin >> salesT;
	

	return salesT;
}
void calcCom(double baseCom, double com, double &salesT)
{
	com = salesT * baseCom;
	

}
void displayCom(double totalC, double com, double &salesT)
{
	cout << "Commission for this salesman: " << fixed << setprecision(2) << com << endl << endl;
}
void displayTotCom(char totalChar, double totalC, double com, double &salesT)
{
	cout << "Do you want to know the total amount of all commissions?(Y for yes, N for no) ";
	cin >> totalChar;
 
	if (toupper(totalChar) == 'Y') {
		totalC += com;
		cout << "Total of all commissions: $" << fixed << setprecision(2) << totalC << endl << endl;
	}
}
Last edited on
Line 19,33: What is the purpose of passing total to totalSales()? It is not used by totalSales().

Line 22: What is the purpose of the if? You've already determined that sales is >= 0 in the while statement.

Line 24: What is the point of calculating commission here? Isn't that what calcCom is supposed to do?

Line 42: You're passing salesT by reference and returning it. Lines 19 & 33 you ignore the return value, so what's the point of returning it? Not the best practice to do it both ways.

Line 50: com is passed by value. It's value is lost when the function exists. You should pass it by reference if you want the the caller's value updated.

Line 60: totalChar should not be an argument. It should be a local variable. It is not needed outside the function.


Hey, thanks for your help. I tried to follow your suggestions but it didn't really do much in terms of making the code do anything different. It still won't update the commission. Here's my updated code:

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <iomanip>
using namespace std;

//function prototypes
double totalSales(double &salesT);
void calcCom(double baseComis, double &com, double &salesT);
void displayCom(double totalC, double &com, double &salesT);
void displayTotCom(char totalChar, double totalC, double &com, double &salesT);

int main() {

	const double baseCom = 0.1;
	double commission = 0.0;
	double totalCom = 0.0;
	double sales = 0.0;
	char total = ' ';

	totalSales(sales);

	while (sales >= 0) {
			calcCom(baseCom, commission, sales);
			displayCom(totalCom, commission, sales);
			for (totalCom = 0.0; totalCom >= 0.0; totalCom += commission) {
				displayTotCom(total, commission, totalCom, sales);

				totalSales(sales);
				displayCom(totalCom, commission, sales);
			}
		}



	return 0;
}

//function definitions
double totalSales(double &salesT)
{
	cout << "What is the total sales amount? ";
	cin >> salesT;
	

	return salesT;
}
void calcCom(double baseComis, double &com, double &salesT)
{
	com = salesT * baseComis;
	

}
void displayCom(double totalC, double &com, double &salesT)
{
	cout << "Commission for this salesman: " << fixed << setprecision(2) << com << endl << endl;
}
void displayTotCom(char totalChar, double totalC, double &com, double &salesT)
{
	cout << "Do you want to know the total amount of all commissions?(Y for yes, N for no) ";
	cin >> totalChar;
 
	if (toupper(totalChar) == 'Y') {
		totalC += com;
		cout << "Total of all commissions: $" << fixed << setprecision(2) << totalC << endl << endl;
	}
}
Topic archived. No new replies allowed.