Salesperson's commission

So this is due tonight, but I could still use insight on how to make it better in the future.
The code's purpose is to calculate and display each salesperson's commission, (10%) and then to add up the total commissions of each salesperson and display the total. So far, I've gotten the program to run, but for some reason the program keeps giving me 0 for the commission and the total commission. I need to figure out how to get the code to actually give me a 10% commission off of numbers like 110, 15000, and 1000000
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
#include <iostream>
#include <iomanip>
using namespace std;

//function prototypes
double amountSold(double sales);
void calculateCommission(double firstCommission, double sales, double commissionPercent);
void displayCommission(double firstCommission);
void calculateTotalCommission(double totalCommission, double firstCommission);
int main()
{
	//declare variables
	double sales = 0.0;
	double commissionPercent = 0.1;
	double firstCommission = 0.0;
	double totalCommission = 0.0;
	char anotherSale = ' ';

	//enter the variables
	cout << "Would you like to see a salesperson's commission? (Y/N)" << endl;
	cin >> anotherSale;
	do{
		cout << "Enter the total sales:" << endl;
		cin >> sales;
		sales = amountSold(sales);
		//display first commission
		displayCommission(firstCommission);
		cout << "Would you like to review another sale? (Y/N)" << endl;
		cin >> anotherSale;
	} while (toupper(anotherSale) == 'Y');

	//calculate and display the total commission
	calculateTotalCommission(totalCommission, firstCommission);
	cout << "The total commission is:" << totalCommission << endl;
	return 0;
}//end main function
//***function definitions***
double amountSold(double sales)
{
	return sales;
}//end of amount sold
void calculateCommission(double firstCommission, double sales, double commissionPercent)
{
	firstCommission = sales * commissionPercent;
}//end of calculate commission
void displayCommission(double firstCommission)
{
	cout << "The commission for this salesperson is" << firstCommission << endl;
}//end of display commission
void calculateTotalCommission(double totalCommission, double firstCommission)
{
	totalCommission += firstCommission;
}//end of total commission 
line 25 does nothing. sales is passed to amountsold() by value and returned by value.

Nothing has changed firstCommission in the do loop so its still 0.0

You need to call calculateCommision() in the do loop. And the function needs to return a double.
All those variables in calculatCommision() evaporate after the function ends. You need to return the firstcommssion value from that function. It is NOT the same variable as the firstcommision variable in your main function. They are completely different.

I don't know what your function amountSold() is for unless its a place saver. But given the other code I don't think its a place saver but a mistake. It does nothing.
You need to review scope. The variables in functions are local in scope and disappear after the function ends. It doesn't matter if they have the same name as variables in other functions ( like main). They are distinctly different. They are local to their function. You need to return a value from those functions and assign it to a variable in your main function. Otherwise you lose it and firstCommission will remain 0.0 just as you initialized it.
Last edited on
I fixed it for you:

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
#include <iostream>
#include <iomanip>

using namespace std;

//function prototypes
double amountSold(double sales);
double calculateCommission(double sales, double commissionPercent);
void displayCommission(double firstCommission);
void calculateTotalCommission(double totalCommission, double firstCommission);
int main()
{
	//declare variables
	double sales = 0.0;
	double commissionPercent = 0.1;
	double firstCommission = 0.0;
	double totalCommission = 0.0;
	char anotherSale = ' ';

	//enter the variables
	cout << "Would you like to see a salesperson's commission? (Y/N)" << endl;
	cin >> anotherSale;
	do{
		cout << "Enter the total sales:" << endl;
		cin >> sales;
		sales = amountSold(sales); // this does nothing and can be commented out
		                 //display first commission
		firstCommission = calculateCommission(sales, commissionPercent);
		totalCommission += firstCommission; //add to total commission
		displayCommission(firstCommission);
		cout << "Would you like to review another sale? (Y/N)" << endl;
		cin >> anotherSale;
	} while (toupper(anotherSale) == 'Y');

	//calculate and display the total commission
	//  calculateTotalCommission(totalCommission, firstCommission); //this is wrong. If a function is needed (and it isn't)
	                                                                // it should be in the do loop and return a double.
	cout << "The total commission is:" << totalCommission << endl;
	return 0;
}//end main function
//***function definitions***
double amountSold(double sales)
{
	return sales;
}//end of amount sold
double calculateCommission(double sales, double commissionPercent)
{
	return sales * commissionPercent;
}//end of calculate commission
void displayCommission(double firstCommission)
{
	cout << "The commission for this salesperson is" << firstCommission << endl;
}//end of display commission
void calculateTotalCommission(double totalCommission, double firstCommission)
{
	totalCommission += firstCommission;
}//end of total commission  
Thank you very much, sir. I will need to study this rigorously. Your help is most appreciated. Thanks again, have a good night.
Anytime! The thing is that program requires no functions. All the manipulations are basic mathematical operations, inputs and outputs. At times , spurious ( unnecessary) functions make code more readable. In this case, I think it makes the code less readable. But if its a school assignment, the purpose may be to get you used to calling functions. So if its the assignment to use functions for all of this, I can show you code to do that. But if you wrote this thinking that is the proper way to do this? No its not and I can show you the proper way to do it. Good code is clear code. Always.
Thanks again, sir! I will have to consult you later, in that case. My final exam is coming up and I must study.

I assume this what shawnlau meant by the proper way to do it?

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
#include <iostream>

using namespace std;

int main()
{
	double sales = 0.0;
	double commissionPercent = 0.1;
	double firstCommission = 0.0;
	double totalCommission = 0.0;
	char anotherSale;


	do{
	    cout << "Enter the total sales:";
            cin  >> sales;
            firstCommission = sales * commissionPercent;
            totalCommission += firstCommission;
	    cout << "The commission for this salesperson is" << firstCommission << endl;
	    cout << "The total commission is:" << totalCommission << endl;
            cout << "Would you like to review another sale? (Y/N)";
            cin  >> anotherSale;
	}while (toupper(anotherSale) == 'Y');
    return 0;
}

Last edited on
Yes, thats it. But I don't know if the object of the lesson is function calls. If so, that's what its for, but if he wrote that accomplish the task, it requires no functions.
Topic archived. No new replies allowed.