Hello, I was curious as to what would be suggested as the best menu code. It needs to be able to return to the menu if they enter a wrong key. I am making a game on my spare time to better myself with c++ and thought this would be a fun way to go about it. Anyways, I have been trying to mess with a do while loop and have some scrap code for you to look at and tell me if I am going in the correct direction.
void showMenu()
{
......
//this is the menu
return ;
}
//other functions....
int main()
{
showMenu(); //show menu the fist time
//Enter input
cin>>choice;
switch(choice)
{
case 1:
beginGame();
break;
case 2:
goToStore();
break;
......
case n:
endGame();
break;
default:
//you have entered wrong menu item
system("cls"); //clear screen
showMenu(); //show menu again
}
system("pause");
return 0;
}
That makes a little sense. I am still roughly learning functions. Also, is it alright to use system("cls"); ? I heard it is not good to use in it programming.
Alright, thank you for the answer. Would you happen to know how to make such a program in to an actual program. Ex(You click on an icon and it executes) I'm just curious because when I finish I have a few people who would like to try it and I would rather not have to show them all how to get a compiler onto their pc.
Alright, so I have been messing around with that menu you showed me and I am running into a problem with it not looping back and reshowing the menu if you enter something incorrect.
#include <iostream>
#include <cstdlib>
usingnamespace std;
void showMenu()
{
cout << "1.)Start Game " << endl
<< "2.)End Game " << endl;
}
int beginGame()
{
cout << "This is the beginning of the game." << endl;
}
int goToStore()
{
cout << "This is the store." << endl;
}
void endGame()
{
cout << "Ending Game" << endl;
}
int main()
{
int choice;
showMenu(); //show menu the first time
//Enter input
cin>>choice;
switch(choice)
{
case 1:
beginGame();
break;
case 2:
goToStore();
break;
case 0:
endGame();
break;
default:
//you have entered wrong menu item
system("cls"); //clear screen
showMenu(); //show menu again
}
system("pause");
return 0;
}