Alternatives for goto statement

I have a been in a bad programming practice. Could you guys tell me alternatives for goto statement.
Loops?
Could you show the code you worked on so people can give specific examples?
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
#include <iostream>
#include <string>
#include <conio.h>

//Otal J.,Damary J.,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();
}
I'll just make 1 block so you get an Idea what you could do

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
// replace
		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;
		}

// with

// before code
bool Moderator(int& mod, int& honorarium1) // pass by reference!
{
    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"); 
        return false;
    }
    return true;
}

/// in code
while(!Moderator(mod, honorarium1));


that's just 1 way of doing it but it's much better than using goto
Topic archived. No new replies allowed.