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.