Alternitave for GOTO?

OK so I am a beginner to programming and trying to figure out how to not use the goto code as i understand it has issues. I understand that I can use a while but not sure how as I will need to jump to 2 parts of code in my int main.

Any help would be greatly appreciated code is below.
I know my code may be a bit messy in some places but at the moment I am trying to sort out these goto's as something else.

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
#include <iostream>
using namespace std;
// Superclass
class job 
{
private:
	// declare variables
	float labour, travel, plasticpipes, copperpipes, chromepipes, Total;
public:
	// set funtions
	void set_labour(float lbr) { labour = lbr * 40;}
	void set_travel(float tvl) { travel = tvl* 1;}
	void set_plastic (float ptc) { plasticpipes = ptc * 2;}
	void set_copper (float cpr) { copperpipes = cpr * 3;}
	void set_chrome (float crm) { chromepipes = crm * 4;}
	// welcomescreen funtion
	void welcomescreen()
	{
		system("CLS");
		cout << "--------Job Estimater------- " << endl;
	}
	// invoiceprompt output/input funtion
	void invoiceprompt()
	{
		cout << "PRESS 1 TO SHOW INVOICE" << endl;
		cout << "PRESS 2 TO CLEAR SCREEN AND GOTO WELCOME PAGE" << endl;
	}
	// invalidkey output funtion
	void invalidkey()
	{
		cout << "Invalid Key entered! please try again" << endl;

	}
	// settotal function
	void settotal()
	{
	Total = labour + travel + plasticpipes + copperpipes +chromepipes;
	}
	void showinvoice()
	{
	//call total function	
	settotal();
	void displayoutput();
	//display invoice
	system("CLS");
	cout <<"############## Job Estimator - Invoice  ###############" << endl;
	cout << "Labour is " << labour/40 << " hours @ " << (char)156 << "40 per hour = " << (char)156 << labour << endl;
	cout << "-------------------------------------------------------" << endl;
	cout << "Travel is " << travel/1 << " miles @ " << (char)156 << "1 per mile = " << (char)156 << travel << endl;
	cout << "-------------------------------------------------------" << endl;
	cout << "Plastic pipes are " << plasticpipes/2 << " metres @ " << (char)156 << "2 per metre =" << (char)156 << plasticpipes << endl;
	cout << "-------------------------------------------------------" << endl;
	cout << "Copper pipes are " << copperpipes/3 << " metres @ " << (char)156 << "3 per metre =" << (char)156 << copperpipes << endl;
	cout << "-------------------------------------------------------" << endl;
	cout << "Chrome pipes are " << chromepipes/4 << " metres @ " << (char)156 << "4 per metre =" << (char)156 << chromepipes << endl;
	cout << "#######################################################" << endl;
	cout << "-------------------------------------------------------" << endl;
	cout << "######### The total cost is *** " << Total << " Pounds *** #########" << endl;
	cout << "-------------------------------------------------------" << endl;
	cin.get();
	}
	
		
};
// Subclass
class Job1 : public job
{
private:
	float lbl, tvl, ptc, cpr, crm;
	
public:
	
	
	// labour input/output funcion
	void labouroutinset(float lbr)
	{
		cout << "How many hours have you worked? " << endl;
		cin >> lbr;
		set_labour(lbr);

	}
	// travel input/output function
	void traveloutinset(float tvl)
	{
		cout << "How many miles are you travelling?" << endl;
		cin >> tvl;
		set_travel(tvl);
	}
	void plasticpipesoutinset(float ptc)
	{
		cout << "How many metres of plastic pipes will be needed?" << endl;
		cin >> ptc;
		set_plastic(ptc);
	}
	void copperpipesoutinset(float cpr)
	{
		cout << "How many metres of copper pipes will be needed?" << endl;
		cin >> cpr;
		set_copper(cpr);
	}
	void chromepipesoutinset(float crm)
	{
		cout << "How many metres of chrome pipes will be needed?" << endl;
		cin >> crm;
		set_chrome(crm);
	}
		
};

int main ()
{
	Job1 job2;
	// declare variables
	float lbr = 0, tvl = 0 , ptc = 0 , cpr = 0 , crm = 0;
	int option = 0;
	
start1:
	// call welcomescreen funtion
	job2.welcomescreen();

	// Labour Input/ouput/set
	job2.labouroutinset(lbr);
	job2.welcomescreen();
	

	// Travel Input/output/set
	job2.traveloutinset(tvl);
	job2.welcomescreen();

	// Plastic Pipes Input/output/set
	job2.plasticpipesoutinset(ptc);
	job2.welcomescreen();

	// Copper Pipes Input/output/set
	job2.copperpipesoutinset(cpr);
	job2.welcomescreen();

	// Chrome Pipes Input/output
	job2.chromepipesoutinset(crm);
	job2.welcomescreen();
start:
	// Show invoice prompt
	job2.invoiceprompt();
	cin >> option;

	// if input = 1 show invoice
	if (option == 1)
	{
		job2.showinvoice();
		goto start;
	}

	// if input = 2 clear screen and goto welcome page
	else if (option == 2) 
	{
		system("CLS");
		goto start1;
	}

	// If input does not equal 1 or 2 diplay error and prompt retry
	else if (option != 1 || 2) 
	{
		job2.invalidkey();
		goto start;
	}		
	};
	

	
closed account (zb0S216C)
A switch[1] is the closest, and most safest, variation of goto. Like goto, it jumps to the best matching case. Here's a sample:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
enum Card_Suit
{
    spade, heart, club, diamond
} card(spade);

switch(card)
{
    case spade:
        // Do something...
        break;

    case heart:
        // Do something...
        break;

    //...
}

It's worth noting that the only values a switch accepts are integers; that is, anything the evaluates to a integral value, excluding floating-point values.

References:
[1] http://www.cplusplus.com/doc/tutorial/control/


Wazzak
I am sorry but I don't even understand that i don't get how to keep asking to reinput it seems that the condition would just end up to be more if statements please try to explain it more basic if you can thanks or try to link it with my code if you dont mind.
closed account (zb0S216C)
Sorry, I shouldn't have used enum - I just complicated things. You can use a simple loop. Loops are used to repeat code over again until a certain condition is met. Here's an analogy: You place a jug of beans within a microwave and then press the start button. While those beans aren't hot, wait until they are hot. This can be seen like this in code:

1
2
3
4
5
bool hot(false);
while(!hot)
{
    // Wait until they are hot...
}

Now, you would use a loop to keep asking the user for input either if the input is wrong, or you need more. For example:

1
2
3
4
5
6
7
int input(0);

while(input < 100)
{
    std::cout << "Enter a number greater than or equal to 100: ";
    std::cin >> input;
}

This code will continue to ask the user to enter a value until their input is greater than or equal to 100. If it's not, the loop will ask the user again. The only time the loop will stop is when:

1) ...the user gets bored because they can't enter a number bigger than 100
2) ...when the computer rusts
3) ...when there's a power-cut

A switch can appear within a loop. A switch is used to handle specific values.

Wazzak
Last edited on
You could use an if/elseif/else statement.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
if(expression)
{
job1()
}

else if(expression)
{
job2()
}

else
{
...
}



Another alternative is a nested switch statement.
1
2
3
4
5
6
7
8
9
10
11
switch(expression)
{
case [condition 1]:
job1(); break;

case [condition 2]:
job2(); break;

default:
//function for error throwing
}
Thanks guys I understand it now.
Topic archived. No new replies allowed.