Help with while loops (repetition structures)

I'm not understanding why I'm getting so many errors. Is there a simpler way I can code this?
The payroll manager at Kenton Incorporated wants a program that allows him to enter an unknown number of payroll amounts for each of three stores: store 1, store 2, store 3. The program should calculate the total payroll and then display the result on the screen.

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>
using namespace std;

int main()
{
	//declare variables
	int totalPayroll = 0;
	int store1 = 0;
	int store2 = 0;
	int store3 = 0;
	int id = 0;
	int tempPayroll = 0;

	while
	{
		cout << "Enter store id (1,2,3) or 0 to quit: ";
		cin >> id;

		if (id == 0) //sentinal value
		{
			break;
		}
		else if (id == 1)
		{
			cout << "Enter payroll amount for store " << id;
			cin >> tempPayroll;
			totalPayroll = totalPayroll + tempPayroll;
			store1 = store1 + tempPayroll;
		}
		else if (id == 2)
		{
			cout << "Enter payroll amount for store " << id;
			cin >> tempPayroll;
			totalPayroll = totalPayroll + tempPayroll;
			store2 = store2 + tempPayroll;
		}
		else if (id == 3)
		{
			cout << "Enter payroll amount for store " << id;
			cin >> tempPayroll;
			totalPayroll = totalPayroll + tempPayroll;
			store3 = store3 + tempPayroll;
		}
	}

	cout << "\nTotal Payroll for store 1: " << store1;
	cout << "\nTotal Payroll for store 2: " << store2;
	cout << "\nTotal Payroll for store 3: " << store3;
	cout << "\nTotal Payroll of all stores: " << totalPayroll;

	system("pause");
	return 0;
}
Hello megaland!

After checking your code, I realized that your code is written perfectly except for one thing, that you obviously have forgotten about it...
You have to put a condition between parenthesis right beside the while, because your while loop will stop when reaching the condition your stated!

1
2
3
4
while(condition!)
{
// Your loop;
}


That's it! Good Luck!
What would my condition be though?
You would want a do-while loop. Not a while loop. Put all of 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
cout << "Enter store id (1,2,3) or 0 to quit: ";
		cin >> id;

		if (id == 0) //sentinal value
		{
			break;
		}
		else if (id == 1)
		{
			cout << "Enter payroll amount for store " << id;
			cin >> tempPayroll;
			totalPayroll = totalPayroll + tempPayroll;
			store1 = store1 + tempPayroll;
		}
		else if (id == 2)
		{
			cout << "Enter payroll amount for store " << id;
			cin >> tempPayroll;
			totalPayroll = totalPayroll + tempPayroll;
			store2 = store2 + tempPayroll;
		}
		else if (id == 3)
		{
			cout << "Enter payroll amount for store " << id;
			cin >> tempPayroll;
			totalPayroll = totalPayroll + tempPayroll;
			store3 = store3 + tempPayroll;
		}


Between this -

do
{

}while(id != 0);

That will run the loop until the user inputs the number 0, then the program will print out all of the information on the screen, and quit, just like you want it to.
Last edited on
Topic archived. No new replies allowed.