is there an easier way to write this code??
without using enum,
enum MenuState{MenuPlay,MenuHelp,MenuExit};
MenuState ms=MenuPlay;
char ch=0;//or anything other than '\r'
while (ch!='\r')//IIRC getch returns '\r' when user hits enter
{
switch (ms)
{
case MenuPlay:
cout<<"--> PLAY\n HELP\n EXIT\n";
break;
case MenuHelp:
cout<<" PLAY\n--> HELP\n EXIT\n";
break;
case MenuExit:
cout<<" PLAY\n HELP\n--> EXIT\n";
break;
default: //always provide default, some compilers will not compile until it is there
cout<<"ERROR: Unknown state!\n";
}
ch=getch();//process more input
switch (ch)
{
case 'w':
case 'W':
ms=MenuPlay;
break;
case 'e':
case 'E':
ms=MenuHelp;
break;
case 'r':
case 'R':
ms=MenuExit;
break;
default:
}
}
//now that we are out of the loop we know that the user hit enter. now we just check ms
switch (ms)
{
case MenuPlay:
//play was selected
break;
case MenuHelp:
//help was selected
break;
case MenuExit:
//exit was selected
break;
}