Learning switch and function call returns

Hello all. I'm trying to make a menu loop by using while (function != 0) function(); I'm returning a value of 0 for my exit command but it goes through my exit routine and starts back over, not exiting... all other menu options are working fine and loop properly.

On the switch, case 4 is the one I'm having a problem with. I even changed the function to an int and made a cout to get the return value and it was bringing a 0, yet the main function continued looping one last time. If I chose 4 twice in a row, it would exit.

I'm new here, and I'm not sure if posting my entire code is considered rude or something, so I hope you'll forgive me if it is.

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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351


			
#include <iostream>
#include <cmath>
#include <iomanip>
#include <Windows.h>
#include <cstdlib>

using namespace std;

const double PI = 3.14159;


				//Calculation functions

double circle_calc(float x)
{
	return (PI * pow(x, 2.0));
}

float rectangle_calc(float x, float y)
{	
	return (x * y);
}

float triangle_calc(float x, float y)
{
	return (x * y * .5);
}


				//Organizes user's choice

int circle()			//user chooses circle
{
	float radius;
	cout << "Please enter the radius of the circle: \n" <<
		fixed << showpoint << setprecision(2);
	cout << "Radius: ";
	cin >> radius;

	if (radius < 0)
	{
		cout << "Please enter a valid number.\n";
		system("pause");
		return 1;
	}

	else
	{
		cout << "The area of this circle is: " << circle_calc(radius) << endl;
		system("pause");
		system("cls");
		return 0;
	}
}

int rect()			//user chooses rectangle
{
	float length, width;
	cout << "Please enter the lenth and width of the rectangle: \n" <<
		fixed << showpoint << setprecision(2);
	cout << "Length: ";
	cin >> length;
	cout << "\nWidth: ";
	cin >> width;

	if (length < 0)
	{
		cout << "Please enter a valid number.\n";
		system("pause");
		return 1;
	}

	else if (width < 0)
	{
		cout << "Please enter a valid number.\n";
		system("pause");
		return 1;
	}

	else cout << "The area of this rectangle is: " << rectangle_calc(length, width) << endl;

	system("pause");
	system("cls");
	return 0;
}

int tri()			//user chooses triangle
{
	float length, width;
	cout << "Please enter the lenth and width of the triangle: \n" <<
		fixed << showpoint << setprecision(2);
	cout << "Length: ";
	cin >> length;
	cout << "\nWidth: ";
	cin >> width;

	if (length < 0)
	{
		cout << "Please enter a valid number.\n";
		system("pause");
		return 1;
	}

	else if (width < 0)
	{
		cout << "Please enter a valid number.\n";
		system("pause");
		return 1;
	}

	else cout << "The area of this triangle is: " <<
		triangle_calc(length, width) << endl;

	system("pause");
	system("cls");
	return 0;
}

				//exit functions chosen randomly

void exit_func1()			//exits with flair!
{
	int exit_loop = 10, exit_switch;

	while(exit_loop >= 0)
	{
		int exit_col = 0;

		cout << setw(20) << right << " ";
		switch(exit_loop)
			{
			case 0 : cout << " ";
			case 1 : cout << " ";
			case 2 : cout << " ";
			case 3 : cout << " ";
			case 4 : cout << " ";
			case 5 : cout << " ";
			case 6 : cout << " ";
			case 7 : cout << " ";
			case 8 : cout << " ";
			case 9 : cout << " ";
			case 10 : cout << " ";
			}

		while (exit_col <= exit_loop)
			cout << right << exit_col++ << " ";
		
		cout << endl;
		exit_loop--;
		Sleep(500);
	}
	Sleep(2000);
}

void exit_func2()			//exits with flair!
{
	int exit_loop = 10, exit_switch;

	//loop columns
	while(exit_loop >= 0)
	{
		int exit_col = 0;

		cout << setw(20) << right << " ";
		switch(exit_loop)
			{
			case 0 : cout << ". . . . . . . . . . .";
				break;
			case 1 : cout << ". . . . . . . . . .";
				break;
			case 2 : cout << ". . . . . . . . .";
				break;
			case 3 : cout << ". . . . . . . .";
				break;
			case 4 : cout << ". . . . . . .";
				break;
			case 5 : cout << ". . . . . .";
				break;
			case 6 : cout << ". . . . .";
				break;
			case 7 : cout << ". . . .";
				break;
			case 8 : cout << ". . .";
				break;
			case 9 : cout << ". .";
				break;
			case 10 : cout << ".";
				break;
			}

		while (exit_col <= exit_loop)
			cout << right << exit_col++ << " ";
		
		cout << endl;
		exit_loop--;
		Sleep(500);
	}
	Sleep(2000);
}

void exit_func3()			//exits with flair!
{
	int exit_loop = 10, exit_switch;

	//loop columns
	while(exit_loop >= 0)
	{
		int exit_col = 0;

		cout << setw(20) << right;

		while (exit_col <= exit_loop)
		{
			switch(exit_loop)
			{
			case 0 : cout << "           ";
				break;
			case 1 : cout << "          ";
				break;
			case 2 : cout << "         ";
				break;
			case 3 : cout << "        ";
				break;
			case 4 : cout << "       ";
				break;
			case 5 : cout << "      ";
				break;
			case 6 : cout << "     ";
				break;
			case 7 : cout << "    ";
				break;
			case 8 : cout << "   ";
				break;
			case 9 : cout << "  ";
				break;
			case 10 : cout << " ";
				break;
			}

			cout << right << exit_col++ << " ";
		}
		cout << endl;
		exit_loop--;
		Sleep(500);
	}
	Sleep(2000);
}


void exit_func()			//randomly chooses which exit function you see.
{
	int randnum, low = 1, high = 100;

	srand(time(0));
	randnum = (rand() % (low - high + 1)) + low;

	cout << randnum;
	Sleep(500);
	cout << "\b\b  \n";
	if (randnum >= 67)
		exit_func1();
	else if (randnum > 33 && randnum < 67)
		exit_func2();
	else exit_func3();
}


				//prompts user for desired function
int user_choice()
{
	int choice;
	
	for (int x = 0; x < 25; x++)
		cout << "* ";
	cout << endl <<
		"*\tPlease Choose your desired function:\t*\n" << 
		"* 1. Calculate the Area of a Circle\t\t*\n" <<
		"* 2. Calculate the Area of a Rectangle\t\t*\n" <<
		"* 3. Calculate the Area of a Triangle\t\t*\n" <<
		"* 4. Quit \t\t\t\t\t*\n";
	for (int x = 0; x < 25; x++)
		cout << "* ";
	cout << endl <<	"\n\tEnter Your Choice (1-4):\t";
	cin >> choice;

	switch(choice)			//Actual choice function
	{
		case 1 :			//If the user chooses circle.
		{
			cout << "You chose " << choice << "!\n\t";		
			circle();
			cin.clear();
			system("cls");
			return 1;
		}	
		break;

		case 2 :			//If the user chooses rectangle
		{
			cout << "You chose " << choice << "!\n\t";
			rect();
			cin.clear();
			system("cls");
			return 1;
		}
		break;

		case 3 :			//If the user chooses triangle
		{
			cout << "You chose " << choice << "!\n\t";
			tri();
			cin.clear();
			system("cls");
			return 1;
		}
		break;

		case 4 :			//If the user chooses to quit
		{
			cout << "Thanks for using my program!!!\n\t";
			cin.clear();
			exit_func();
			return 0;
		}
		break;

		default:			//If the user chooses an invalid option
		{
			cout << "please choose 1 through 4...\n\t";
			cin.clear();
			system("pause");
			system("cls");
			return 1;
		}
		break;
	}
}


int main()
{
	while (user_choice() != 0)	
		user_choice();
		

	return 0;
}
Last edited on
try this

1
2
3
4
5
6
int main()
{
	while (user_choice() != 0);
		
	return 0;
}
Apparently I missed some fundamental part of the While statement. Yanson, your response works perfectly, thank you very much!

Dithanial.
Topic archived. No new replies allowed.