Do-While Loop Problem?

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


#include <iostream>
#include <iomanip>
using namespace std;
void main()
{
	cout << endl;
	cout << setw(34)<<"Weekly Payroll Report \n";
	cout << "------------------------------------------- \n\n";

	int EmployeeNum;
	double GrossPay , WithHoldings, TotalGrossPay=0 , TotalWithHoldings=0 , TotalNet=0 ;

	do
	{
		cout << "Enter your Employee Number (0 to quit): ";
		cin >> EmployeeNum;

		while ( EmployeeNum < 0 )
		{
			cout << "Sorry. Employee Number are positive. Enter again: ";
			cin >> EmployeeNum;
		}

		cout << "Enter gross pay: ";
		cin >> GrossPay;

		while ( GrossPay <= 0 )
		{
			cout << "Gross must be larger than 0 \n";
			cout << "Enter gross pay: ";
			cin >> GrossPay;
		}

		TotalGrossPay += GrossPay;

		cout << "Enter withholdings: ";
		cin >> WithHoldings; 

		while ( WithHoldings > GrossPay )
		{
			cout << "The Withholding may not be larger than the gross pay. \n";
			cout << "Enter withholdings: ";
			cin >> WithHoldings;
		}

		TotalWithHoldings += WithHoldings;
		TotalNet = TotalGrossPay - TotalWithHoldings;

	}
    while ( EmployeeNum != 0);

	cout << fixed << setprecision(2) << endl; 
	cout << "Total Gross Pay: " << setw(15) << "$ " << setw(12) << TotalGrossPay << endl;;
	cout << "Total Withholdings: " << setw(12) << "$ " << setw(12) << TotalWithHoldings << endl;
	cout << "Total Net: " << setw(21) << "$ " << setw(12) << TotalNet << endl; 
}



The sentinal value "while (EmployeeNum != 0)" works but only after it executes one loop. I don't know why it's doing it. For instance.

Weekly Payroll Report
-------------------------------------------

Enter your Employee Number (0 to quit): 0
// It should stop here!
Enter gross pay: 2
Enter withholdings: 1

Total Gross Pay: $ 2.00
Total Withholdings: $ 1.00
Total Net: $ 1.00
Press any key to continue . . .

As you can see, it does not terminate right away. It continues to do one loop. Help? Thank you. It has to be a do while!!
Last edited on
Are you in a c++ at Webster?
The reason it doesn't stop right there is because a do loop is required to run completely through at least one time. It doesn't check the value of EmployeeNum until it reads while(EmployeeNum != 0);

Enter your Employee Number (0 to quit): 0
Enter gross pay: 2
Enter withholdings: 1

//THIS is the first time it checks to see if EmployeeNum != 0

Total Gross Pay: $ 2.00
Total Withholdings: $ 1.00
Total Net: $ 1.00


Hope this helps.
Topic archived. No new replies allowed.