Returning in a menu

What I am trying to make is an opening menu for a game. It's in working condition but not ideal. I listed most of the menu options as cases in a switch. I'm just more comfortable using switches then if and else. I planned to put for loops around it and only escape them by pressing the number 4 and ENTER. What I was trying to do was give the player the ability to go back to the previous menu. Like in a real game by pressing O or something. I tried various for loops and breaks and continues but it always ends up a mess. Is the for loop the wrong path?

int main()
{
cout << "Fading Death" << endl;
cout << "1 = Start" << endl;
cout << "2 = Difficulty" << endl;
cout << "3 = Credits" << endl;
cin >> choice;
switch (choice)
{
/*1*/case Start:
{
cout << "1 = New Game" << endl;
cout << "2 = Resume Game" << endl;
cout << "3 = Load Game" << endl;
cout << "4 = Back" << endl;
cin >> choice2;
switch (choice2)
{
/*A*/case NewGame:
cout << "Starting a new game..." << endl;
cout << "Select a difficulty." << endl;
cout << "1 = Easy" << endl;
cout << "2 = Normal" << endl;
cout << "3 = Hard" << endl;
cout << "4 = Back" << endl;
cin >> choice3;
switch (choice3)
{
/*a*/case Easy:
cout << "Saving..." << endl;
break;
/*b*/case Normal:
cout << "Saving..." << endl;
break;
/*c*/case Hard:
cout << "Saving..." << endl;
break;
}
break;

/*B*/case ResumeGame:
cout << "Choose a file to resume..." << endl;
break;
/*C*/case LoadGame:
cout << "Choose a file to load..." << endl;
break;
}
break;
/*2*/case Difficulty:
cout << "Select a difficulty." << endl;
cout << "1 = Easy" << endl;
cout << "2 = Normal" << endl;
cout << "3 = Hard" << endl;
cout << "4 = Back" << endl;
cin >> choice3;
switch (choice3)
{
/*A*/case Easy2:
cout << "Saving..." << endl;
break;

/*B*/case Normal2:
cout << "Saving..." << endl;
break;

/*C*/case Hard2:
cout << "Saving..." << endl;
break;
}
}
break;
/*3*/case Credits: //Credits done!
{
cout << "Created by: Daniel Murano" << endl;
cout << "4 = Back" << endl;
cin >> choice2;
break;
}
break;
default:
cout << "Incorrect input." << endl;
}
return 0;
}

Note: I don't expect it to actually load or resume a game. I'm just trying to learn how it's done.
Please use code tags and proper indentation. Long and nested code is a pain to look at when it's like this.

I'd suggest to wrap every menu inside a loop, then:
- before each switch, put and if with a break inside that is executed if the"go back" input in given. Since it's outside of the switch it will affect the loop execution
Or
- use a bool to control if the loops must execute. When you want to go back set the value to false

I'm not really sure about how you want to control the flow of the menus, so these two provide a very basic solution
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int main()
{
FirstMenu:
cout << "Fading Death" << endl;
cout << "1 = Start" << endl;
cout << "2 = Difficulty" << endl;
cout << "3 = Credits" << endl;
cin >> choice;
switch (choice)
{
case 1: //start
cout << "1 = New Game" << endl;
cout << "2 = Resume Game" << endl;
cout << "3 = Load Game" << endl;
cout << "4 = Back" << endl;
cin >> choice2;
switch (choice2)
{
case 4:
goto FirstMenu
...
...
..


using goto is unsafe but it gonna help you...
Last edited on
Maeriden is a genius. I wrapped the whole code in a for loop then put if, else before each switch. Now whenever the number four is entered it brings the user back to the first menu. I'm still working on making more nested loops within but the break keyword doesn't seem to exit the for loop. Perhaps because it's within the switch it thinks the break is meant for the switch rather then the loop? Anyways, this was a great accomplishment. Thanks again!
Topic archived. No new replies allowed.