Inserting a loop in a switch statement

I am making a payroll system and i want to make sure that the persons option is well used in this switch statement


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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
code below:
#include <iostream>
#include <string>
#include <conio.h>


void Monthly();
void Annual();
void Last();
void Exit();

using namespace std;

int main ()
{
string user,pass;
int attemptCounter = 0;
char choice1;

cout << "Welcome and Good Day! Please login before you proceed.";
cout << "\n3 ATTEMPTS ONLY.";

while ( attemptCounter < 3 )
{

cout << "\n\n\tUSERNAME: ";
cin >> user;
cout << "\n\tPASSWORD: ";
cin >> pass;

if ( user != "user" || pass != "1234" )
{
    cout << "\n\tINCORRECT user/password Try again!" << "\n" <<
    endl;

    attemptCounter++;
}

else
{

cout << "Correct User and Password!";
cout << "\nYou may proceed.";
system("cls");
break;

}
}


if (user == "user" || pass =="1234")
{
cout << "Welcome! Please select your choice." <<endl; 

cout << "\n\n\nMAIN MENU:\n\n\t(1) Monthly Payroll Report\n\t(2) Annual Payroll Report\n\t(3) EXIT"; 
cout << "\n\n\tInput Choice: ";
cin >> choice1;

switch(choice1)
{
               case '1':
               cout << "\n\nYou choosed Monthly Payroll Report.";
               Monthly();
			   break;
               
               
               case '2':
               cout << "\n\nYou choosed Annual Payroll Report.";
               Annual();
			   break;
               
               
               case '3':
               cout << "\n\nYou choosed to Exit.";
               Exit();
               break;
               
               
               default:
               cout << "\n\nINVALID OUTPUT!";
               cout << "\nPlease input your option once more." << endl;
               break;              
}
}
else 
{
	system("cls");
	cout << "I'm so sorry but, you already reached 3 attempts. The program will exit now.";
}
getch();
}
void Monthly()
{
	string first,last,idnum;
	int netpay,bonus,month,stat2,unided,stat,civil,hours;
	int overtime,statax,mod,honorarium1,honorarium2,honorarium;
	int mod2,med,medin,SSS=350,Phil=100,PI=50,basic=32000;
	int earnings,deductions;
	
	stage1:
	system("cls");
	cout << "\t\t\t>>>>>>>>Monthly Payroll Report<<<<<<<<";
	cout << "\n(1)January\n(2)Febuary\n(3)March\n(4)April\n(5)May\n(6)June\n(7)July\n(8)August\n(9)September\n(10)October\n(11)November\n(12)December\n\n(NEVER INPUT A ALPHABET IF YOU DON'T WANT TO RESTART)";
	cout << "\n\nInput The Current Month: ";
	cin >> month;
	if(month>0 && month<=12)
	{
		system("cls");
		cout << "\t\t\t>>>>>>>>Monthly Payroll Report<<<<<<<<";
		cout << "\n\nInput your First Name: ";
		cin >> first;
		cout << "\n\nInput your Last Name: ";
		cin >> last;
		system("cls");
		cout << "\t\t\t>>>>>>>>Monthly Payroll Report<<<<<<<<";
		cout << "\n\nInput your ID Number: ";
		cin >> idnum;
		system("cls");
		Status:
		cout << "\t\t\t>>>>>>>>Monthly Payroll Report<<<<<<<<";
		cout << "\n\n(1)Fulltime or (2)Parttime";
		cout << "\n\nInput your Employment Status: ";
		cin >> stat;
		if(stat>0 && stat<2)
		{
			cout << "\n\nYour Employment Status Is Full Time" << endl;
			system("pause");
			Union:
			system("cls");
			cout << "\t\t\t>>>>>>>>Monthly Payroll Report<<<<<<<<";
			cout << "\n\n(1)Yes\n(2)No";
			cout << "\n\nAre you Union? ";
			cin >> stat2;
			if(stat2>0 && stat2<2)
			{
				unided=100;
			}
			else if(stat2>1 && stat2<3)
			{
				unided=0;
			}
			else
			{
				cout << "\n\nInvalid Answer, Please Try Again" << endl;
				system("pause");
				goto Union;
			}
		}
		else if(stat>1 && stat<3)
		{
			cout << "\n\nYour Employment Status Is Part Time." << endl;
			system("pause");
		}
		else
		{
			cout << "Invalid Answer, Please try Again.";
			system("cls");
			goto Status;
		}
		system("cls");
		cout << "\t\t\t>>>>>>>>Monthly Payroll Report<<<<<<<<";
		cout << "\n\nYour Basic month salary is " << basic << ".00 Pesos." << endl;
		system("pause");
		CivilStatus:
		system("cls");
		cout << "\t\t\t>>>>>>>>Monthly Payroll Report<<<<<<<<";
		cout << "\n\n(1)Single\n(2)Married";
		cout << "\n\nInput Your Civil Status: ";
		cin >> civil;
		if(civil>0 && civil<2)
		{
			cout << "\n\nYou Are Single." << endl;
			system("pause");
			statax=0.07;			
		}
		else if(civil>1 && civil<3)
		{
			cout << "\n\nYou Are Married." << endl;
			system("pause");
			statax=0.05;
		}
		else
		{
			cout << "\n\nInvalid Answer. Please Try Again." << endl;
			system("pause");
			goto CivilStatus;
		}
		system("cls");
		cout << "\t\t\t>>>>>>>>Monthly Payroll Report<<<<<<<<";
		cout << "\n\nHow Many Hours Did You Stay For Overtime? ";
		cin >> hours;
		overtime=hours*200;
		system("pause");
		Moderator:
		system("cls");
		cout << "\t\t\t>>>>>>>>Monthly Payroll Report<<<<<<<<";
		cout << "\n\n(1)Yes\n(2)No";
		cout << "\n\nAre You Class Moderator? ";
		cin >> mod;
		if(mod>0 && mod<2)
		{
			cout << "\n\nYou Are Class Moderator." << endl;
			system("pause");
			honorarium1=1000;		
		}
		else if(mod>1 && mod<3)
		{
			cout << "\n\nYou Are Not Class Moderator." << endl;
			system("pause");
			honorarium1=0;
		}
		else
		{
			
			cout << "\n\nInvalid Answer. Please Try Again." << endl;
			system("pause");
			goto Moderator;
		}
		ClubMod:
		system("cls");
		cout << "\t\t\t>>>>>>>>Monthly Payroll Report<<<<<<<<";
		cout << "\n\n(1)Yes\n(2)No";
		cout << "\n\nAre You Club Moderator? ";
		cin >> mod2;
		if(mod2>0 && mod2<2)
		{
			cout << "\n\nYou Are Club Moderator." << endl;
			system("pause");
			honorarium2=500;		
		}
		else if(mod2>1 && mod2<3)
		{
			cout << "\n\nYou Are Not Club Moderator." << endl;
			system("pause");
			honorarium2=0;
		}
		else
		{
			cout << "\n\nInvalid Answer. Please Try Again." << endl;
			system("pause");
			goto ClubMod;
		}
		MedInsurance:
		system("cls");
		cout << "\t\t\t>>>>>>>>Monthly Payroll Report<<<<<<<<";
		cout << "\n\n(1)Yes\n(2)No";
		cout << "\n\nDo You Have Medical Insurance? ";
		cin >> med;
		if(med>0 && med<2)
		{
			cout << "\n\nYou Have Medical Insurance." << endl;
			system("pause");
			medin=350;		
		}
		else if(med>1 && med<3)
		{
			cout << "\n\nYou Don't Have Medical Insurance." << endl;
			system("pause");
			medin=0;
		}
		else
		{
			cout << "\n\nInvalid Answer. Please Try Again." << endl;
			system("pause");
			goto MedInsurance;
	 	}
		system("cls");
		cout << "\t\t\t>>>>>>>>Monthly Payroll Report<<<<<<<<";
		cout << "\n\nResult: ";
		
}
}
void Annual()
{
}
void Last()
{
	int last;
	
	system("cls");
	cout << "\n\n(1)Go Back To Main Menu\n(2)Exit The Program";
	cout << "\n\nWhat Will You Do Next? ";
	cin >> last;
	if(last>0 && last<2)
	{
		cout << "\n\nOk, Sure!";
		system("cls");
		main();
	}
	else if(last>1 && last<3)
	{
		cout << "\n\nThank You For Using This Payroll System!" << endl;
		system("pause");
		Exit();
	}
	else
	{
		cout << "\n\nInvalid Answer! Try Again" << endl;
		system("pause");
		Last();
	}
}
void Exit()
{
	system("cls");
	cout << "\n\n\n\t\t\t\tThank You For your time! Have A Good Day.";
getch();
}

Last edited on
This part?

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
switch(choice1)
{
               case '1':
               cout << "\n\nYou choosed Monthly Payroll Report.";
               Monthly();
			   break;
               
               
               case '2':
               cout << "\n\nYou choosed Annual Payroll Report.";
               Annual();
			   break;
               
               
               case '3':
               cout << "\n\nYou choosed to Exit.";
               Exit();
               break;
               
               
               default:
               cout << "\n\nINVALID OUTPUT!";
               cout << "\nPlease input your option once more." << endl;
               break;              
}


Looks good to me. Although, you dont need a break after the default, so I would remove that.
Last edited on
yes that part but how if when i run it the default part does not let the person have a second chance to choose as it is said "please input your option once more"
Last edited on
Well, writing cout << "Please finish the rest of the program for me Mr code" << endl;

Wont make the program finish itself. You need some kind of loop.

Edit: Try putting this -
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
cout << "Welcome! Please select your choice." <<endl; 

cout << "\n\n\nMAIN MENU:\n\n\t(1) Monthly Payroll Report\n\t(2) Annual Payroll Report\n\t(3) EXIT"; 
cout << "\n\n\tInput Choice: ";
cin >> choice1;

switch(choice1)
{
               case '1':
               cout << "\n\nYou choosed Monthly Payroll Report.";
               Monthly();
			   break;
               
               
               case '2':
               cout << "\n\nYou choosed Annual Payroll Report.";
               Annual();
			   break;
               
               
               case '3':
               cout << "\n\nYou choosed to Exit.";
               Exit();
               break;
               
               
               default:
               cout << "\n\nINVALID OUTPUT!";
               cout << "\nPlease input your option once more." << endl;
               break;              
}


inside this loop -

1
2
3
4
do
{

 } while (choice1 != '3');


That will keep running the switch statement until the user enters 3, which will then ofc exit the progarm.
Last edited on
thanks it really helps me but i dont understand why "!= 3" not "!=4"
to begin with, you dont have a "4" so what will it mean if you use it? you use "3" Because in case 3, you have said that you want it to exit. so "!='3'" means, Keep running this loop until the user decides to exit. If it hits default, doesnt matter go back to the beginning of the loop. User inputs 1? do whatever case 1 does, then get the fuck back in line bitch. User enters 3? Ohh thats my queue to gtfo of this loop, peace out suckers, and the program exists.
Last edited on
Thanks But when I Hit 3 or Exit it went to the void but then the loop still continued. Why???
and for me its good to curse.
Last edited on
Ive tried your code and it exists for me, you must be doing something wrong, can you show your current code for the switch statement? (all of it, including the loop and the if statements arround it)
Here is it sir



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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328

#include <iostream>
#include <string>
#include <conio.h>

//Otal ,Damary,Kim J.	
//9-Canisius - Payroll System

void Monthly();
void Annual();
void Last();
void Exit();

using namespace std;

int main ()
{
string user,pass;
int attemptCounter = 0;
char choice1;

cout << "Welcome and Good Day! Please login before you proceed.";
cout << "\n3 ATTEMPTS ONLY.";

while ( attemptCounter < 3 )
{

cout << "\n\n\tUSERNAME: ";
cin >> user;
cout << "\n\tPASSWORD: ";
cin >> pass;

if ( user != "user" || pass != "1234" )
{
    cout << "\n\tINCORRECT user/password Try again!" << "\n" <<
    endl;

    attemptCounter++;
}

else
{

cout << "Correct User and Password!";
cout << "\nYou may proceed.";
system("cls");
break;

}
}


if (user == "user" || pass =="1234")
{
	do{
cout << "Welcome! Please select your choice." <<endl; 

cout << "\n\n\nMAIN MENU:\n\n\t(1) Monthly Payroll Report\n\t(2) Annual Payroll Report\n\t(3) EXIT"; 
cout << "\n\n\tInput Choice: ";
cin >> choice1;

switch(choice1)
{
               case '1':
               cout << "\n\nYou choosed Monthly Payroll Report.";
               Monthly();
			   break;
               
               
               case '2':
               cout << "\n\nYou choosed Annual Payroll Report.";
               Annual();
			   break;
               
               
               case '3':
               cout << "\n\nYou choosed to Exit.";
               Exit();
               break;
               
               
               default:
               cout << "\n\nINVALID OUTPUT!";
               cout << "\nPlease input your option once more." << endl;
               break;              
    }
    
}while(choice1 != 3);
  
}
else 
{
	system("cls");
	cout << "I'm so sorry but, you already reached 3 attempts. The program will exit now.";
}
getch();
}
void Monthly()
{
	string first,last,idnum;
	int netpay,bonus,month,stat2,unided,stat,civil,hours;
	int overtime,statax,mod,honorarium1,honorarium2,honorarium;
	int mod2,med,medin,SSS=350,Phil=100,PI=50,basic=32000;
	int earnings,deductions;
	
	stage1:
	system("cls");
	cout << "\t\t\t>>>>>>>>Monthly Payroll Report<<<<<<<<";
	cout << "\n(1)January\n(2)Febuary\n(3)March\n(4)April\n(5)May\n(6)June\n(7)July\n(8)August\n(9)September\n(10)October\n(11)November\n(12)December\n\n(NEVER INPUT A ALPHABET IF YOU DON'T WANT TO RESTART)";
	cout << "\n\nInput The Current Month: ";
	cin >> month;
	if(month>0 && month<=12)
	{
		system("cls");
		cout << "\t\t\t>>>>>>>>Monthly Payroll Report<<<<<<<<";
		cout << "\n\nInput your First Name: ";
		cin >> first;
		cout << "\n\nInput your Last Name: ";
		cin >> last;
		system("cls");
		cout << "\t\t\t>>>>>>>>Monthly Payroll Report<<<<<<<<";
		cout << "\n\nInput your ID Number: ";
		cin >> idnum;
		system("cls");
		Status:
		cout << "\t\t\t>>>>>>>>Monthly Payroll Report<<<<<<<<";
		cout << "\n\n(1)Fulltime or (2)Parttime";
		cout << "\n\nInput your Employment Status: ";
		cin >> stat;
		if(stat>0 && stat<2)
		{
			cout << "\n\nYour Employment Status Is Full Time" << endl;
			system("pause");
			Union:
			system("cls");
			cout << "\t\t\t>>>>>>>>Monthly Payroll Report<<<<<<<<";
			cout << "\n\n(1)Yes\n(2)No";
			cout << "\n\nAre you Union? ";
			cin >> stat2;
			if(stat2>0 && stat2<2)
			{
				unided=100;
			}
			else if(stat2>1 && stat2<3)
			{
				unided=0;
			}
			else
			{
				cout << "\n\nInvalid Answer, Please Try Again" << endl;
				system("pause");
				goto Union;
			}
		}
		else if(stat>1 && stat<3)
		{
			cout << "\n\nYour Employment Status Is Part Time." << endl;
			system("pause");
		}
		else
		{
			cout << "Invalid Answer, Please try Again.";
			system("cls");
			goto Status;
		}
		system("cls");
		cout << "\t\t\t>>>>>>>>Monthly Payroll Report<<<<<<<<";
		cout << "\n\nYour Basic month salary is " << basic << ".00 Pesos." << endl;
		system("pause");
		CivilStatus:
		system("cls");
		cout << "\t\t\t>>>>>>>>Monthly Payroll Report<<<<<<<<";
		cout << "\n\n(1)Single\n(2)Married";
		cout << "\n\nInput Your Civil Status: ";
		cin >> civil;
		if(civil>0 && civil<2)
		{
			cout << "\n\nYou Are Single." << endl;
			system("pause");
			statax=0.07;			
		}
		else if(civil>1 && civil<3)
		{
			cout << "\n\nYou Are Married." << endl;
			system("pause");
			statax=0.05;
		}
		else
		{
			cout << "\n\nInvalid Answer. Please Try Again." << endl;
			system("pause");
			goto CivilStatus;
		}
		system("cls");
		cout << "\t\t\t>>>>>>>>Monthly Payroll Report<<<<<<<<";
		cout << "\n\nHow Many Hours Did You Stay For Overtime? ";
		cin >> hours;
		overtime=hours*200;
		system("pause");
		Moderator:
		system("cls");
		cout << "\t\t\t>>>>>>>>Monthly Payroll Report<<<<<<<<";
		cout << "\n\n(1)Yes\n(2)No";
		cout << "\n\nAre You Class Moderator? ";
		cin >> mod;
		if(mod>0 && mod<2)
		{
			cout << "\n\nYou Are Class Moderator." << endl;
			system("pause");
			honorarium1=1000;		
		}
		else if(mod>1 && mod<3)
		{
			cout << "\n\nYou Are Not Class Moderator." << endl;
			system("pause");
			honorarium1=0;
		}
		else
		{
			
			cout << "\n\nInvalid Answer. Please Try Again." << endl;
			system("pause");
			goto Moderator;
		}
		ClubMod:
		system("cls");
		cout << "\t\t\t>>>>>>>>Monthly Payroll Report<<<<<<<<";
		cout << "\n\n(1)Yes\n(2)No";
		cout << "\n\nAre You Club Moderator? ";
		cin >> mod2;
		if(mod2>0 && mod2<2)
		{
			cout << "\n\nYou Are Club Moderator." << endl;
			system("pause");
			honorarium2=500;		
		}
		else if(mod2>1 && mod2<3)
		{
			cout << "\n\nYou Are Not Club Moderator." << endl;
			system("pause");
			honorarium2=0;
		}
		else
		{
			cout << "\n\nInvalid Answer. Please Try Again." << endl;
			system("pause");
			goto ClubMod;
		}
		MedInsurance:
		system("cls");
		cout << "\t\t\t>>>>>>>>Monthly Payroll Report<<<<<<<<";
		cout << "\n\n(1)Yes\n(2)No";
		cout << "\n\nDo You Have Medical Insurance? ";
		cin >> med;
		if(med>0 && med<2)
		{
			cout << "\n\nYou Have Medical Insurance." << endl;
			system("pause");
			medin=350;		
		}
		else if(med>1 && med<3)
		{
			cout << "\n\nYou Don't Have Medical Insurance." << endl;
			system("pause");
			medin=0;
		}
		else
		{
			cout << "\n\nInvalid Answer. Please Try Again." << endl;
			system("pause");
			goto MedInsurance;
	 	}
		system("cls");
		cout << "\t\t\t>>>>>>>>Monthly Payroll Report<<<<<<<<";
		cout << "\n\nResult: "<<"\n Month:"<< month<<"\nFull Name:"<<first<<last<<"\nID Number:"<<idnum<<"\nEmployment Status:"<<stat<<"\nCivil  Status:"<<civil<<"\nBsic Monthly Salary:"<<basic<<"\nEarnings:"<<basic+honorarium+overtime<<"\nDeductions:"<<SSS+Phil+PI<<"\nNet Pay:"<<basic+honorarium+overtime-SSS-Phil-PI<<"\n";
		
		system("pause");
		Last();
	}
}
void Annual()
{
	
	
	
	
	
	
}
void Last()
{
	int last;
	
	system("cls");
	cout << "\n\n(1)Go Back To Main Menu\n(2)Exit The Program";
	cout << "\n\nWhat Will You Do Next? ";
	cin >> last;
	if(last>0 && last<2)
	{
		cout << "\n\nOk, Sure!";
		system("cls");
		main();
	}
	else if(last>1 && last<3)
	{
		cout << "\n\nThank You For Using This Payroll System!" << endl;
		system("pause");
		Exit();
	}
	else
	{
		cout << "\n\nInvalid Answer! Try Again" << endl;
		system("pause");
		Last();
	}
}
void Exit()
{
	system("cls");
	cout << "\n\n\n\t\t\t\tThank You For your time! Have A Good Day.";
getch();
}





Here is your problem - while(choice1 != 3);

"choice1" is a character, not an integer. Here you are telling the loop to run until the user inputs the integer 3. Which he will never do because the user only inputs characters. You want to add these ' ' on the 3. So it looks like this - while(choice1 != '3');

if you look closely at the first time I gave you the loop it had those already :p
Yeah I used " " not ' ' sorry my bad but sir I have another problem it is the goto and my teacher says not to use goto but I did. How will i get rid of this goto using your given solution or their is a special solution.
hmm yeh goto is very bad programming practice and you shouldnt really use it. It has one good use and thats exiting nested loops.

I never learned how to use goto because Its shit, so unfortunately I have no idea how it works or how to change it into something better.

Make a new post asking of alternative uses to goto. And only show a part of your code that uses goto (and where it goes) instead of the entire code.

Best of luck to you :)

Edit: make sure to mark the thread as solved first.
Last edited on
Thanks sir I will try my best sir
Note: Deadline is today sir.HAHAHa

wait you male? because i am calling you sir
Im not sure what I am, I havent explored my body in a while.
Topic archived. No new replies allowed.