To getting the right output-NESTED LOOPS

QUESTION 2: NESTED LOOPS

Company ABC employs a number of salesmen to sell a variety of items. These salesmen earn their income from a commission and are paid based on the items they sell. Company ABC sells several different lines of merchandise
according to grades, namely A, B, C, D and E. Grade A products are difficult to sell (such as a flat screen television) and the salesman receive a higher commission if they sell items from this grade. On the other hand,
products that are graded at E (such as a magazine), are widespread and do not require any creative sales techniques.

Commission is determined as follows:
Grades CommissionGrade A 20% of the total sales
Grade B 18% of the total sales
Grade C 15% of the total sales
Grade D 10% of the total sales
Grade E 5% of the total sales

After the gross salary (i.e. salary before deductions) for each salesman is calculated, a portion of this salary is taxed according to the following tax categories:
Category Current rate Gross salary
Super income tax rate 50% is greater than or equal to R20 000.00
High income tax rate 40% is less than R20 000.00 but greater than or equal to
R15 000.00
Mid income tax rate 30% is less than R15 000 but greater than or equal to
R10 000.00
Low income tax rate 20% is less than R10 000.00 but greater than equal to
R5 000.00
Lowest income tax rate 10% is below R5 000.00

In addition to paying taxes, the salesmen have to contribute a portion of their salary towards their medical aid scheme. Some salesmen qualify for a tax rebate of 50% if their medical aid contribution is greater than or equal to 10% of their gross salary and if their salary is less than R5000.00.

Write a C++ program to determine the tax, gross salary and the final net salary received by each salesman in Company ABC. The program should display these details in the form of a pay slip for each salesman. Additionally
the program should display the total amount in sales made for the month.
The program has the following structure:
· Totals are initialised; totalSales and grossSalary (gross salary for each salesman).
· A prompting message for the number of salesmen at Company ABC.
· A for loop iterating over the number of salesmen.
· Inside this for loop - a prompting message is displayed requesting the user to enter the salesman's staff
number.
· A while loop validates the staff number which can only be between 999 and 9999.
· A prompt for the salesmen's medical aid contribution that will be deducted from each salesman's salary.
· Inside the outer for loop is another inner for loop going from one to five corresponding to the different
grades of products sold. Inside this for loop is a switch statement which is used for prompting the user to input the total sales in rands for each grade of merchandise sold.
· A nested if statement follows to determine the amount of tax that will be deducted from each salesman's salary.
· The tax rebate is determined based on the medical aid contribution.
· A payslip for the salesman is displayed, indicating the gross salary, tax deduction, medical aid contribution deduction and the net salary.
· When the outer for loop is exited, the total sales made for the month is displayed.
A partial implementation of the above program specification is given below. The lines where you have to insert or complete code are indicated in bold.
Note 1: ...Indicates that a line of code needs to be completed.
//*** Indicates that more than one line of code needs to be completed.
:
Program 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <iostream>
using namespace std;
int main()
{
int numSalesmen;
int staffNumber;
float commission;
float tax;
float netSalary;
float medicalAid;
float gradeSales;
float totalSales = 0;
float grossSalary = 0;
const float superIncomeTaxRate = 0.5;
const float highIncomeTaxRate = 0.4;
const float midIncomeTaxRate = 0.3;
const float lowIncomeTaxRate = 0.2;
const float lowestIncomeTaxRate = 0.1;
const float gradeACommission = 0.2;
const float gradeBCommission = 0.18;
const float gradeCCommission = 0.15;
const float gradeDCommission = 0.1;
const float gradeECommission = 0.05;

//***prompt for number of salesman
//for loop iterate over number of salesmen
for(...;...;...)
{
cout<<"Enter the staff number for salesman " << ... << " : ";
cin >> staffNumber;
//validate staff number
while (...)
{
//***prompt for staff number
}
cout<< "Enter the medical aid contribution for staff number " << ... << " : ";
cin >> medicalAid;
//for loop iterating over the grades
for (...;...;...)
{
switch (...)
{
case 1 : cout << "Enter total sales for Grade A:";
commission = gradeACommission;
break;
//*** complete switch statement
}//end switch statement
cin >> gradeSales;
//determine gross salary
grossSalary = grossSalary + gradeSales*commission;
//determine total sales
totalSales = ...
}//end inner for-loop
//nested if statement to determine taxes
if (grossSalary >= 20000.00)
tax = tax = grossSalary*superIncomeTaxRate;
else if (...)
tax = ...;
else if (...)
tax = ...;
else if (...)
tax = ...;
else
tax = grossSalary*lowestIncomeTaxRate;
//determine if salesman gets rebate based on medical aid contribution
if (medicalAid >= (0.1*grossSalary) && ...)
{
//rebate
tax = tax*0.5;
}
//determine net salary
netSalary = ...;
cout.setf(ios::fixed);
cout.precision(2);
//Please see Note 2 below for an explanation of cout.width, and a preview of the
//payslip
cout << ".................PAY SLIP....................."
<< endl;
cout << "Staff Number : # " ;
cout.width(10);
cout << ... << endl;
cout << "Gross Salary : R " ;
cout.width(10);
cout << ... << endl;
cout << "Tax Deducted : R " ;
cout.width(10);
cout << ... << endl;
cout << "Medical Aid : R " ;
cout.width(10);
cout << ... << endl;
cout << "Net Salary : R " ;
cout.width(10);
cout << ... << endl;
cout << ".............................................."
<< endl <<endl;
//reset grossSalary to 0 for next salesman
grossSalary = ...;
}//end of outer for-loop
//display total sales
cout<<" Total Sales for this month : R " << ... << endl;
return 0;
}


See comment....
Last edited on
with the following input:
Enter the number of salesman for Company ABC : 2
Enter the staff number for salesman 1 : 1002
Enter the medical aid contribution for staff number 1002 : R200.00
Enter total sales for Grade A: R20000.00
Enter total sales for Grade B: R1000.00
Enter total sales for Grade C: R1000.00
Enter total sales for Grade D: R2000.00
Enter total sales for Grade E: R2000.00

Enter the staff number for salesman 2 : 1111
Enter the medical aid contribution for staff number 1111 : R3000
Enter total sales for Grade A: R20000.00
Enter total sales for Grade B: R0
Enter total sales for Grade C: R0
Enter total sales for Grade D: R1000.00
Enter total sales for Grade E: R0
The program should displays a pay slip for each salesman #1111 as follows:
.................PAY SLIP.....................
Staff Number : # 1111
Gross Salary : R 4100.00
Tax Deducted : R 205.00
Medical Aid : R 3000.00
Net Salary : R 895.00
..............................................

with this 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
 cout << "Enter number of saleman for the company  : ";
 cin >> numSalesman;
 
 for (int i =1; i <= numSalesman; i++)
 {
   cout << "Enter the staff number for salesman " << i << " : ";
   cin >> staffNumber;
   
   while (staffNumber < 999 || staffNumber > 9999)
   {
     cout << "re-enter correct staff number for salesman " << i << " : ";
     cin >> staffNumber;
   }
   
   cout << "Enter the medical medical aid  contribution for staff number " << staffNumber << " : ";
   cin >> medicalAid;
   
   for (int c =1; c <= 5; c++)
   {
     switch(c)
     {
       case 1: cout << "Enter total sales for Grade A: ";
       commission = gradeACommission;
       break;
       case 2: cout << "Enter total sales for Grade B: ";
       commission = gradeBCommission;
       break;
       case 3: cout << "Enter total sales for Grade C: ";
       commission = gradeCCommission;
       break;
       case 4: cout << "Enter total sales for Grade D: ";
       commission = gradeDCommission;
       break;
       default:
       cout << "Enter total sales for Grade E: ";
       commission = gradeECommission;
     }
     
     cin >> gradeSales;
     grossSalary = grossSalary + gradeSales * commission;
     totalSales = totalSales + grossSalary;
   }
   
   if (grossSalary >= 20000.00)
      tax = grossSalary * superIncomeTaxRate;
   else if (grossSalary >= 15000.00)
           tax = grossSalary * highIncomeTaxRate;
   else if (grossSalary >= 10000.00)
           tax = grossSalary * midIncomeTaxRate;
   else if (grossSalary >= 5000.00)
           tax = grossSalary * lowIncomeTaxRate;
   else
       tax = grossSalary * lowestIncomeTaxRate;
       
   if ((medicalAid >= (0.1 * grossSalary)) && (grossSalary < 5000.00))
   {
      tax = tax * 0.5;
   }
   
   netSalary = grossSalary - tax;
   cout.setf(ios::fixed);
   cout.precision(2);
   cout << endl;
   cout << ".................PAY SLIP................." << endl;
   cout << "Staff Number      : # " ;
   cout.width(10);
   cout << staffNumber << endl;
   cout << "Gross Salary      : R " ;
   cout.width(10);
   cout << grossSalary << endl;
   cout << "Tax Deducted      : R " ;
   cout.width(10);
   cout << tax << endl;
   cout << "Medical Aid       : R " ;
   cout.width(10);
   cout << medicalAid << endl;
   cout << "Net Salary        : R " ;
   cout.width(10);
   cout << netSalary << endl;
   cout << "..........................................." << endl << endl;
   
   grossSalary = 0.0;
   
 }
 
 cout << " TOtal Sales for this month : R " << totalSales << endl;
 
 return 0;
}


the out put is:
Enter number of saleman for the company  : 2
Enter the staff number for salesman 1 : 1002
Enter the medical medical aid  contribution for staff number 1002 : 200.00
Enter total sales for Grade A: 20000.00
Enter total sales for Grade B: 1000.00
Enter total sales for Grade C: 1000.00
Enter total sales for Grade D: 2000.00
Enter total sales for Grade E: 2000.00

.................PAY SLIP.................
Staff Number      : #       1002
Gross Salary      : R    4630.00
Tax Deducted      : R     463.00
Medical Aid       : R     200.00
Net Salary        : R    4167.00
...........................................

Enter the staff number for salesman 2 : 1111
Enter the medical medical aid  contribution for staff number 1111 : 3000
Enter total sales for Grade A: 20000.00
Enter total sales for Grade B: 0
Enter total sales for Grade C: 0
Enter total sales for Grade D: 1000.00
Enter total sales for Grade E: 0

.................PAY SLIP.................
Staff Number      : #       1111
Gross Salary      : R    4100.00
Tax Deducted      : R     205.00
Medical Aid       : R    3000.00
Net Salary        : R    3895.00
...........................................

 TOtal Sales for this month : R 41870.00
Press any key to continue . . .


the net salary is incorrect. HELP
Be calm, it looks like you've not deducted 'Medical Aid' from 'Gross Salary' :0
Last edited on
Thanx, I'm having such a tough time with this assignment.

 
   netSalary = grossSalary - (tax +medicalAid);


Please look out for more of these. :)
Topic archived. No new replies allowed.