#include<iostream>
usingnamespace std;
int main()
{
int choice;
cout<<"1. go a head\n""2. turn back this menu to pick again 1 or 2\n";
cin>>choice;
switch(choice)
{
case 1:cout<<"somthing";
case 2:// at this line what should i do to turn back to
cout<<"1. go a head\n""2. turn back this menu to pick again 1 or 2\n";
}
}
Here's something I put together that I hope you may find helpful (it is a bit spaghetti but I hope you derive the idea then you can organize it however you like):
#include<iostream>
usingnamespace std;
int main()
{
int choice = 0; // Good practice to always initialize your variables to something
bool menuLoop = true;
bool mainMenu = true;
bool optionsMenu = false;
while (menuLoop) // The iteration statement (as long as menuLoop is true)
{
if (mainMenu)
{
cout << "1. go a head\n""2. turn back this menu to pick again 1 or 2\n";
cin >> choice;
}
else
{
std::cout << "Options menu.\n";
std::cout << "Display a list of options here...\n";
std::cout << "2) Back.\n";
cin >> choice;
}
switch (choice)
{
case 1:
cout << "somthing"; // Do something here (call a function, change menu states, etc...) whatever you want
// For example...
optionsMenu = true; // Activate options menu state
mainMenu = false; // Deactivate main menu state
break; // Don't forget break statements after each case (unless you want to fall through to other cases)
case 2:
optionsMenu = false; // Activate options menu state
mainMenu = true; // Deactivate main menu state
break;
default: // Good to have this as a catch-all
break;
}
}
return 0;
}
You should improve this by adding an enumerator to improve code readability instead of plain integers to represent your states. So something like this would be nicer:
#include<iostream>
usingnamespace std;
int main()
{
int choice = 0; // Good practice to always initialize your variables to something
bool menuLoop = true;
bool mainMenu = true;
bool optionsMenu = false;
enum MenuState { OPTIONS_MENU = 1, MAIN_MENU, EXIT };
while (menuLoop) // The iteration statement (as long as menuLoop is true)
{
if (mainMenu)
{
cout << "1. go a head\n""2. turn back this menu to pick again 1 or 2\n";
cin >> choice;
}
else
{
std::cout << "Options menu.\n";
std::cout << "Display a list of options here...\n";
std::cout << "2) Back.\n";
cin >> choice;
}
switch (choice)
{
case OPTIONS_MENU:
cout << "somthing"; // Do something here (call a function, change menu states, etc...) whatever you want
// For example...
optionsMenu = true; // Activate options menu state
mainMenu = false; // Deactivate main menu state
break; // Don't forget break statements after each case (unless you want to fall through to other cases)
case MAIN_MENU:
optionsMenu = false; // Activate options menu state
mainMenu = true; // Deactivate main menu state
break;
case EXIT:
menuLoop = false; // Shutdown if they enter 3
break;
default: // Good to have this as a catch-all
break;
}
}
return 0;
}