Beginner Trouble - While Loop Requiring Multiple Instances of Input to Return Value

Hello all,

I've just started learning C++ this last weekend. Currently, I'm trying to develop a series of interconnected menus/screens as a template for a video game, with each individual menu/screen as its own function. Additionally, I am using Microsoft Visual C++ 2010 Express as my IDE.

Currently, the program calls up a main menu and prompts the user for an input, 1 to go to a sub-menu, and 2 to terminate the program. However, in order for the program to terminate, it requires the user to input 2 a number of times equal to the number of times the sub-menu function has been called. Here is the code:

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
#include <iostream>

using namespace std;

bool mainmenu ();		//IDE implies these must be declared before execution.
void subscreen ();		//IDE implies these must be declared before execution.

int main ()
{
	mainmenu();
	return(0);
}

bool mainmenu ()
{
	while (true)
	{
		system("CLS");
		cout << "Welcome to the main screen!" << endl;
		cout << "Press 1 to go to subscreen." << endl;
		cout << "Press 2 to exit." << endl;
		char a;
		cin >> a;
		switch (a)
		{
		case '1':
			subscreen();
			break;
		case '2':
			return false;
			break;
		default:
			cout << "Not a valid input.";
			system("CLS");
			main();
		}
	}
}

void subscreen ()
{
	
		system("CLS");
		cout << "Test successful!" << endl;
		cout << "Press 1 to return to main screen." << endl;
		char a;
		cin >> a;
		switch (a)
		{
		case '1':
			mainmenu();
			break;
		default:
			cout << "Not a valid input.";
			system("CLS");
			subscreen();
		}
}


I have tried researching this issue, thinking that the input may be reading the carriage return as a second input, but, as mentioned above, it is not just a matter of inputting 2 twice, but a number of times that depends on how many times the sub-menu function was called.

Clearly there is a mistake in my logic somewhere. Any input/advice would be appreciated. Thank you in advance.
Short answer: place return false either between line 27 and 28 or instead of line 51.

Long answer: You calling subscreen from mainscreen and then mainscreen from subscreen you call stack will look like: main() -> mainscreen() -> subscreen() -> mainscreen() ...
When control returns to mainscrees from sub, it will ask again for input. But there as many instances of mainscreen running as you called subscreen. Better rewrite code to not call functions recursively.

And you can declare dunctions as void and use return:
1
2
3
4
5
void foo()
{
//...
return;
}
@MiiNiPaa
And you can declare functions as void and use return:


1
2
3
4
5
void foo()
{
//...
return;
}


A return is not necessary for a void function.

@Aerodyne09

The break statement only breaks out of the switch or loop that it is in - so don't rely on that if you really want the function to complete. Meaning that you can't have any code after the switch, if you want control to go back to main.

Much better for the function to return something, then you can use a return statement to exit the function early.

DO NOT CALL MAIN

Do not use infinite loops - while (true) - better to use a bool variable to exit the loop:
1
2
3
4
5
6
7
8
bool Quit = false;

while (!Quit) {
//your code

//User wants to quit
Quit = true;
}


Just so you are not tempted - Do not use goto either.

HTH

Ah, thank you so much for the answer and advice. Evidently, I haven't got the concept of the stack nailed down, but I will try and keep this in mind in the future. Placing return in place of line 51 makes much more sense now that you mention it.

Thanks again so much!
Topic archived. No new replies allowed.