I want to code this program without using "goto" statement

Hi guys this is my first post here. I have coded a perfectly working program. But my teacher says that you should code it without using single "goto" statement. It's very complex for me like that. Please can anyone of you re-code it without using "goto" even once?

You may use "do-while", "for" or any other loop.

Here is the program :

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

int main()

{
	cout<<"Hi! Please enter a positive number : ";
A:	int a=0;
	cin>>a;




B:  for ( ; a < 0 ; )
	{
		cout<<" \nSorry, the number you just entered is not postive. \n \n";
		cout<<"Plese re-enter : ";
		cin>>a;
	
	} //End of 1st 'for' loop.


	
	
    for ( ; a == 0 ; )
	{
		int c1=0;
		
		cout<<" \nAre you sure you want to continue with '0'(zero)? \n \n";
C:		cout<<"1 - Yes \n";
		cout<<"2 - No \n \n";
		cout<<"Enter your choice (1 or 2) : ";
		cin>>c1;



		if (c1 == 1)
		{
			goto D;
		}




		if (c1 == 2)
		{
			cout<<" \nPlease re-enter the number : ";
			cin>>a;
			goto B;
		}




		if (c1 != 1  &&  c1 != 2)
		{
			cout<<" \nWrong Choice!! Please select again if you want to continue with '0'(zero). \n \n";
			goto C;
		}

	} //End of 2nd 'for' loop.





    for ( ; a>0 ; )
	{
D:		int c2=0;

		cout<<" \nWhat would you like to do with the selected number? \n \n";
E:		cout<<"1 - Find its factorial (Warning: The entered number should be less than 13 or \n";
		cout<<" \t \t \telse factorial will be calculated wrong!) \n";
		cout<<"2 - Find the sum of all numbers upto the entered number \n \n";
		cout<<"Enter your choice (1 or 2) : ";
		cin>>c2;



        if (c2 == 1)
		{
			
			
			if (a == 0)
			{
				cout<<" \nThe factorial of 0 is : 1 (which is true)";
				goto F;
			}
			


			int facto=1;

			for (int n=1 ; n<=a ; n++)
			{
				facto=facto*n;
			}


			cout<<" \nThe factorial of selected number is : ";
			cout<<facto;
			goto F;
		
		}




		if (c2 == 2)
		{


			if (a == 0)
			{
				cout<<" \nSum of all numbers upto the entered number is : 0";
				goto F;
			}





			int sum=0;
			

			for (int m=1 ; m<=a ; m++)
			{
				sum=sum+m;
			}


			cout<<" \nSum of all numbers upto the entered number is : ";
			cout<<sum;
			goto F;
		
		}



		if (c2 != 1 && c2 != 2)
		{
			cout<<" \nWrong choice!! Choose again : \n \n";
			goto E;
		}
	
	} //End of 3rd 'for' loop.




F:	int c3=0;
	

	
	cout<<" \n \nNow choose what to do : \n \n";
G:	cout<<"1 - Repeat cycle (program) \n";
	cout<<"2 - End program \n \n";
	cout<<"Enter your choice (1 or 2) : ";
	cin>>c3;


	
	if (c3 == 1)
	{
		cout<<" \nPlease enter a positive number : ";
		goto A;
	}





	if (c3 == 2)
	{
		cout<<" \n \t Good Bye and have a nice day. ( Remember : TRUST THE PROGRAMMER!!) \n \n \t \t \t";
	}





	if (c3 != 1  &&  c3 != 2)
	{
		cout<<" \nWrong choice!! Choose again : \n \n";
		goto G;
	}




	return 0;
}


I am expecting that you guys will help me. Waiting for a reply.
I am expecting that you guys will help me.

Unfortunately, we do expect you to do the thinking.

You do need nested loops. For example, repeating overall is the out-most loop which terminates once c3 becomes 2, but getting a valid c3 before that is an inner loop of its own.
We do expect you to do the thinking.


Thanks for replying. And yes of course I'm the one who has to create the logic. Which loop will be more suitable? "for" or "do-while"?
Boy oh boy. >_>

Generally, for statements that need to repeat indefinitely until a certain condition stops being met, a do-white is more suitable. Most of your gotos can be replaced by loops. The one that cannot is F, but another keyword related to loops can be used. Can you guess which?

-Albatross
No, I can't recall any other keyword that can be used for loops. It would be kind enough to tell me that, Albatross. I don't think this program is very tough for you people.
Last edited on
Did you ever learn any statements that allow you to instantly jump out of loops?

Alternatively, you can just set a variable that's part of the condition for your loops to run and your if statements that have goto Fs in them and wait until the loops end naturally.

-Albatross

Last edited on
The only statement that I learned is goto. You can tell me any other that you might know.

Alternatively, you can just set a variable that's part of the condition for your loops to run and your if statements that have goto Fs in them and wait until the loops end naturally.


How? Can you give a short example?
Thanks Albatross and keskiverto. Your "LINK" was a great help.. -_-
Last edited on
Hmm. You may want to read this. http://www.cplusplus.com/doc/tutorial/control/#jump_statements Already posted, sorry.

As for the example, sure. It's a very inelegant example and using one of the statements from the above article is (in my opinion) a cleaner solution, but for the purposes of illustrating what I mean...

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
bool run = true;
int result;
do {
    bool gotvalidresult = false;

    //STUFF HAPPENS HERE.

    if(result == -1) {
        std::cout << "User has identified as a llama. This is unacceptable.\n";
        run = false;
    }

    if(run) {
        //MORE STUFF HAPPENS HERE.
    }

    if (!gotvalidresult && run)
        std::cout << "Computation failed, retrying.\n";
    else if (!gotvalidresult && !run)
        std::cout << "Computation failed, exiting.\n";
    else {
        std::cout << "Result: " << result << ".\n";
        run = false;
    }
} while(run);


I haven't been coding in C++ much recently, so forgive me for any bugs in the snippet.

-Albatross
Last edited on
Thanks Albatross for still replying because I thought this was over. I'll definitely consider it and find a solution.
Last edited on
Topic archived. No new replies allowed.