QUESTION 3: 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 Commission
Grade 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 toR10 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
COS1511/101
43
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
#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 thepayslip
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;
}
Note 2 : The program displays a pay slip for each salesman 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
We have used cout.width() to display and align the amount fields on the payslip. Because width() is a member function, it must be invoked with a cout object. One can set the width of a field, as we have done. We’ve chosen 10, which will be wide enough for the staff number and the amount fields. cout.width() only changes the width of the very next output field and then immediately reverts to the default. The default width of the output will be just enough space to print the number, character, or string in the output buffer. One can change this by using width(). It aligns the amounts properly. You will not be tested on cout.width()in the exam.
Run the program using 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
Please edit your post and make sure your code is [code]between code tags[/code] so that it has syntax highlighting and line numbers, as well as proper indentation.