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;
}

You could use defines instead of the enum on line 1 but that wouldn't be any beneficial than what you have now.

Enum is a way to provide clarity (and type information) to your code. The underlying type is simply some integral type so you could just get rid of the enum altogether and replace with numeric values (by default, enums values begin at 0).

The consequence of that is when you read the code you lose the understanding of the logic. Consider:

switch (x) {
   case 0:
      // ...
   case 1:
      // ...
   case 2:
      // ...
}

Versus something like:

switch (inputValue) {
   case EnterCommand:
      // ...
   case ExitCommand:
      // ...
   case PlayCommand:
      // ...
}

Which of the two of those provides more information to the reader?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.