Goto and return 1

Ok so, I read that goto and return 1 shouldn't be used and although I don't quite understand why, I'll take the word of more experienced programmers for it. Below is a code I've written to pretty much review some basics I've learned. However I used goto once because I don't know how to deal with the loop in any other way and also return 1 to exit.
I'd just like some input from you guys, how would I go without using goto and the loop below and what is a better alternative to return 1. Also if you have any other suggestions to improve my code, I gladly welcome them.
Thanks in advance.

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
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <stdio.h>
#include <string>
#include <sstream>

using namespace std;

void eqOne (short& num, short& x, short& x1, short& x2)
{
	num = (rand() + time(0)) % 20;
	x1 = (-num - 3) * 2;
	x2 = (-x1 - 3) * 2;
	x = (-x2 - 3) * 2;
}
void eqTwo (short& num, short& x, short& x1, short& x2)
{
	num = (rand() + time(0)) % 20;
	x1 = (2 - num) * 2;
	x2 = (2 - x1) * 2;
	x = (2 - x2) * 2;
}


int main()
{
	short y, y1, y2, guess, number;
	unsigned short counter[2] = {0};
	bool done;
	string s;
	
	system("color 1F");	

	done = false;

	eqOne(number, y, y1, y2);

top:
	while(!done)
	{
		while (counter[0] <3)
		{
			cout << fixed << showpoint;
			cout << endl << setw(63) << "Guess the number that logically follows in the pattern" << endl << endl;
			cout << setw(31) << number << setw(6) << y1 << setw(6) << y2 << endl << endl;
			getline(cin, s);
			{
				stringstream input( s );
				input >> guess;
				if (!input)
				{
					cout << setw(31) << "Sorry but '" << s << "' is not a valid integer." << endl;
					input.clear();
					input >> guess;
				}
			}			
	
			if (guess == y)
				{
					counter[1]++;
					cout << endl << setw(44) << "That is correct!" << endl;
					while (counter[1] == 1)
					{
						eqTwo(number, y, y1, y2);
						goto top;
//I basically use goto to run the program again after I switch to the second equation.
					}
					while (counter[1] == 2)
					{
						done = true;
						return 1;
					}
				}
			else
				if (guess != y)
					{
						cout << endl << setw(53) << "Wrong answer, please try again." << endl;
						counter[0]++;
					}
				
		}
		while (counter[0] == 3)
		{
			if (guess != y)
				{
					system("cls");
					cout << setw(43) << "GAME OVER" << endl << endl;
					cout << setw(60) << "Better luck next time, thank you for playing." << endl << endl;
					return 1;
				}
		}
		
	}

	return 0;
}
goto is not the problem. It is the misuse of goto that is the problem. using it like you are a "mad goto-er" means you will get unreadable code.

As for return 1, I think it causes the OS to react differently. I don't know exactly.
Last edited on
My brain hurts too much tonight to suggest something better, but your code is inside-out (hence the need for that goto. However, if anyone ever starts jumping up and down and spouting anti-goto propaganda you can peg him as a loser and move on... That said, goto is evil. (See the C++FAQ-Lite)

For maximum portability, return values from 0 to 255 only. Zero is typically understood as "successful exit" while non-zero is "something went wrong". That isn't always the case though. The OS doesn't care what your program returns (well, most don't) so you can feel free to co-opt the exit code however you like.

Hope this helps too.
This is an example of why goto is evil:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//includes

int main() {
   int a, b, c;
   cin>>a;
   cin>>b;
   cin>>c;
   if(a == 0) goto one;
   else goto two;
   one:if(b == 0) goto nine;
   else goto ten;
   two:if(c == 0) goto six;
   else goto seven;
   nine:if(a > 0) goto two;
   //etc etc
}


Have fun figuring out where a random set of values goes.

Here is an example of goto being used in a "better" way (I think XP):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//includes

int main() {
   //stuff
   if(//stuff) {
      while(//stuff) {
         for(//stuff) {
            if(//stuff) {
               goto escape;
            }
            //stuff
         }
         //stuff
      }
      //stuff
   }
   escape://more stuff
}
Thanks for the replies guys, I can see why goto should be avoided by looking at firedraco's example.
Topic archived. No new replies allowed.