Problem with for / while & nested loops

Hi all, I'm very new at programming, still at introductory level. I'm trying to work out this problem, but I'm not sure what I'm doing wrong. Any assistance will be appreciated.

Here is what the programme needs to do:
1. Initialize variables
2. Prompt for number of Salesman
3. For Loop iterating over the number of salesmen
4. Inside this for loop, a prompting message requesting the salesman staff number
5. a while loop validating the staff number
6. A prompt for the salesman's medical aid contribution
7. Outer for loop, with inner for loop going from one to five corresponding to the different grades of products soled, inside this for loop is a switch statement used to prompt user to input total sales for each grade of merchandise sold
8.a nested if statement to determine amount of tax that will be deducted from each salary
9.a tax rebate determined based on medical aid
10. payslip for the salesman is displayed indicating gross salary, tax deduction,medical aid contribution, net salary.
11.when the outer for loop is exited, the total sales made for the month is displayed.

Here's what I have, however the totals are not adding up correctly.

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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
 #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;

    cout << "Please enter the number of salesmen: " << endl;
    cin >> numSalesmen;
    //for loop iterate over number of salesmen
    for(int n = 1;n <= numSalesmen;n++)
    {
        cout << "Enter the staff number for salesman " << n << " : ";
        cin >> staffNumber;
        //validate staff number
        while(staffNumber <= 999 && staffNumber >= 9999)
        {
            cout << "Please enter the salesman's staff number: " << endl;
            cin >> staffNumber;
        }
        cout << "Enter the medical aid contribution for staff number " <<   staffNumber << " : ";
        cin >> medicalAid;
        //for loop iterating over the grades
        for(int sales = 0;sales < 6;sales++)
                        {

                            cout << "Select 1 for Grade A:" <<endl;
                            cout << "Select 2 for Grade B:" <<endl;
                            cout << "Select 3 for Grade C:" <<endl;
                            cout << "Select 4 for Grade D:" <<endl;
                            cout << "Select 5 for Grade E:" <<endl;
                            cin >> sales;

            switch(sales)
            {
                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;
                case 5:; cout << "Enter total sales for Grade E:";
                commission = gradeECommission;

                                //*** complete switch statement
            }//end switch statement
            cin >> gradeSales;
            //determines gross salary
            grossSalary = grossSalary + gradeSales*commission;
            //determines totalsales
            totalSales += gradeSales;
            }//end inner for-loop
            //nested if statement to determine taxes
            if (grossSalary >= 20000.00)
                tax = grossSalary*superIncomeTaxRate;
            else if (grossSalary < 20000 && grossSalary <= 15000)
                tax = grossSalary*highIncomeTaxRate;
            else if (grossSalary < 15000 && grossSalary >= 10000)
                tax = grossSalary*midIncomeTaxRate;
            else if (grossSalary < 10000 && grossSalary >= 5000)
                tax = grossSalary*lowIncomeTaxRate;
            else
                tax = grossSalary*lowestIncomeTaxRate;

            //determine if salesman gets rebate based on medical aid contribution
            if (medicalAid >= (0.1*grossSalary) && grossSalary < 5000)
            {
                 //rebate
                 tax = tax*0.5;
            }
            // determine net salary
            netSalary = grossSalary - tax;
            cout.setf(ios::fixed);
            cout.precision(2);
            
            //preview of thepayslip
            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;
            //reset grossSalary to 0 for next Salesman
            grossSalary = 0.00
            } // end of outer for-loop
            //display total sales
            cout << "Total sales for this month : R " << totalSales << endl;

            return 0;

}
Does this even compile? Because it shouldnt.

You forgot a semicolon here grossSalary = 0.00

Once I fixed that, totalSales added up fine.

Hi,


I've added the semicolon, (must have accidentally removed it when pasting the code and removing spaces) but the grade sales still don't add up correctly. So if I input an amount for each Grade, it doesn't add them up and then do the necessary calculations to get the correct "Gross Salary".
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
for (int sales = 0; sales < 6; sales++)
		{

			cout << "Select 1 for Grade A:" << endl;
			cout << "Select 2 for Grade B:" << endl;
			cout << "Select 3 for Grade C:" << endl;
			cout << "Select 4 for Grade D:" << endl;
			cout << "Select 5 for Grade E:" << endl;
			cin >> sales;

			switch (sales)
			{
			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;
			case 5:; cout << "Enter total sales for Grade E:";
				commission = gradeECommission;


Lets look at this code. First of all. Why is it a for-loop that runs 6 times?

Second, in each case, you tell them to enter total sales for some grade, but I dont see any input? Shouldnt you have a cin >> something; so they can enter it somewhere?

It runs 6 times because there are 5 Grades, so the for loop will stop running after the 5th to make it false and go to the next instruction. This is only my logic, I'm not sure if that portion is correct.

Check line 72 which falls in the outer for loop, that's where the input is for the grade Sales for each grade:

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

     for(int sales = 0;sales < 6;sales++)
                        {

                            cout << "Select 1 for Grade A:" <<endl;
                            cout << "Select 2 for Grade B:" <<endl;
                            cout << "Select 3 for Grade C:" <<endl;
                            cout << "Select 4 for Grade D:" <<endl;
                            cout << "Select 5 for Grade E:" <<endl;
                            cin >> sales;

            switch(sales)
            {
                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;
                case 5:; cout << "Enter total sales for Grade E:";
                commission = gradeECommission;

                                //*** complete switch statement
            }//end switch statement
            cin >> gradeSales;

I just tested it and grossSalary works as intended, it does the calculations correct, the only problem is that it doesnt output the second decimal.

I input the number 2 in each grade. Which on my calculator should have given me 1.36, but it output 1.3, without the 6.

So you want to do this:

#include <iomanip>

and use std::setprecision(2) This will output your decimals.

cout << std::setprecision(2) << grossSalary << endl;


http://www.cplusplus.com/reference/iomanip/setprecision/
Topic archived. No new replies allowed.