How to use a loop?

How do I make this program continue to ask for a paycode after the previous case is complete? And then also show how many times each case has been used?

Sample Output:

Enter paycode (-1 to end): 3
Commission worker selected.
Enter gross weekly sales: 4000
Commission worker’s pay is $ 478.00

Enter paycode (-1 to end): 2
Hourly worker is selected.
Enter hourly salary: 4.5
Enter the total hours worked: 20
Worker’s pay is $90.00

Enter paycode (-1 to end): -1

Total number of managers paid :0
Total number of hourly workers paid :1
Total number of commission workers paid :1
Total number of piece workers paid :0



I know that there needs to be a loop, but not sure how to use it...


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

int main()
{
	float num1, num2, num3, num4, num5, num6, num7;
	int option_select;
	
	
	cout << "Enter paycode: " << endl;

	//1 for manager
	//2 for hourly workers
	//3 for commission workers
	//4 for pieceworkers
	cout << endl;
	cin >> option_select;
	switch (option_select);
	{
	case 1:
		cout << "Manager worker is selected" << endl;
		cout << "Enter fixed weekly salary" << endl;
		cin >> num1;
		cout << "Manager's pay is: " << num1 << endl;
		break;
	case 2:
		cout << "Hourly worker is selected" << endl;
		cout << "Enter hourly salary: " << endl;
		cin >> num2;
		cout << "Enter amount of hours worked: " << endl;
		cin >> num3;
		if (num3 <= 40)
			cout << "Hourly workers pay is: " << (num2 *num3) << endl;
		else
		if ((num3 - 40) >= 1)
			cout << "Hourly workers pay plus Overtime Pay: " << ((num3 - 40) * (num2 * 1.5) + (40 * num2)) << endl;

		break;
	case 3:
		cout << "Commision worker is selected" << endl;
		cout << "Enter gross weekly sales: " << endl;
		cin >> num5;
		cout << "Commision worker's pay is: " << 250 + (num5 *.057) << endl;
		break;
	case 4:
		cout << "Piece worker is selected"
			cout << "Enter amount paid per item: " << endl;
		cin >> num6;
		cout << "Enter amount of items produced: " << endl;
		cin >> num7;
		cout << "Piece Worker's pay is: " << num6 *num7 << endl;

		break;
	default:
		cout << "The entered code is not found " << endl;
	} // end of switch
	 

	system("pause");
	return 0;
} // end of the main 
Hi @brookeann1694,
This is an
example!

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
//CLoop.cpp
//##

#include <iostream>
using std::cout;
using std::cin;
using std::endl;





int main(){

        int number;

        cout<<"Enter non-negative number(-1 to quit): ";
        cin>>number;

        while(number!=-1){ //while number is NOT -1
                if(number%2==0)
                        cout<<'\n'<<number<<" is even."<<endl;
                else
                        cout<<'\n'<<number<<" is odd."<<endl;


                cout<<"Enter non-negative number(-1 to exit): ";
                cin>>number;
        }//end while

return 0; //indicates success
}//end of main
./CLopp 
Enter non-negative number(-1 to quit): 1

1 is odd.
Enter non-negative number(-1 to exit): 4

4 is even.
Enter non-negative number(-1 to exit): 7

7 is odd.
Enter non-negative number(-1 to exit): -1
A do-while loop would be ideal for this, since you want to at least run the code once. You'll need to add another option to exit the loop which will also be your loop condition. It can be faily simple like this:

1
2
3
4
5
6
7
do
{
//your code starting from "Enter paycode"
//add something like "5 to exit"
//all the way to the end of your switch
}
while(option_select != 5);


This will loop your code until the user enters '5' as the paycode.

To keep track of how many times an option's been selected, just add a variable to each switch case that increments every time it is selected. It can look something like this:

1
2
3
case 1:
option1++;
//more code 


Then you can simply output the results once the loop is exited.
Annatar wrote:
A do-while loop would be ideal for this, since you want to at least run the code once.


I disagree, just because the code has to be run once is not a sole justification IMO for using a do loop. All 3 forms of the loops can be converted to one of the other forms, I would prefer a while loop for this, with a bool variable to control when the loop exits.

} while(option_select != 5);

This will exit the loop when it is equal to -1, -2, 5, 6, 7 etc, and will override whatever is specified in the default: case, so it is not a good end condition.

Search this forum for the posts I have written about this: bool controlled while loop

Hope all is well at your end :+)
#include <iostream>
using namespace std;
int main()
{
int option_select;
int option1;
float num1, num2, num3, num4, num5, num6, num7, num8;
do
{

//select an option 1-4
cout << "Enter a selection code (-1 to end): " << endl;
cout << endl;
cin >> option_select;
switch (option_select)
{
case 1:
cout << "You selected Manager" << endl;
cout << "Enter in your salary ";
cin >> num6;
cout << "The total = $" << num6 << endl;
break;

case 2:
cout << "You selected hourly employee" << endl;
cout << "Enter your wage ";
cin >> num1;
cout << "Enter your hours ";
cin >> num2;
if (num2 <= 40)
cout << "Total pay = $" << (num1 * num2) << endl;
else
if ((num2 - 40) >= 1)
cout << "Total pay with overtime = $" << ((num2 - 40) * (num1 * 1.5) + (40 * num1)) << endl;
break;

case 3:
cout << "You selected Commission employee" << endl;
cout << "Enter your gross weekly sales ";
cin >> num4;
cout << "Enter your hours ";
cin >> num5;
cout << "Total hour =" << num5 << endl;
cout << "The total = $" << ((num4 *.057) + 250) << endl;
break;

case 4:
cout << "You selected Pieceworker employee" << endl;
cout << "Enter in number of items produced ";
cin >> num7;
cout << "Enter in price per item ";
cin >> num8;
cout << "Number of items produced = " << num7 << endl;
cout << "Price per item = $" << num8 << endl;
cout << "The total = $" << (num7 * num8) << endl;
break;

case -1:
cout << "ended program " << endl;
break;

default:
cout << "The entered code was not found" << endl;
break;
//end of switch
}
}
while (option_select !=-1);
system("pause");
return 0;
}
Topic archived. No new replies allowed.