0

大家好,我目前在使 switch 语句回到菜单开头时遇到了一些麻烦。相反,它会进入另一个我不希望它进入的主要功能,除非用户选择正确的选择。

无论如何,这是我的代码:

int main() 
{
int choice;
bool menu = true;
cout <<"Please select one of the following options:  \n";

cout << "1: Play\n"
        "2: Help\n"
        "3: Config\n"
        "4: Quit\n";

cout << "Enter your selection (1, 2,3 or 4): ";
cin >> choice;
//*****************************************************************************
// Switch menu to display the menu.
//*****************************************************************************
    if(menu)
    {

      switch (choice)
     {
          case 1:
            cout << "You have chosen play";
            break;
         case 2:
            cout << "You have chosen help\n";
            cout << "Here is a description of the game Hangman and how it is played:\nThe word to guess is represented by a row of dashes, giving the number of letters, numbers and category. If the guessing player suggests a letter or number which occurs in the word, the other player writes it in all its correct positions";
            break; 
         case 3:
            cout << "You have chosen config";
            break;
             case 4:
            cout << "You have chosen Quit, Goodbye.";
            break;
        default:
            cout<< "Your selection must be between 1 and 4!\n";

    }

        }    
getchar();
getchar();


cout << "You missed " << playGame("programming");
cout << " times to guess the word programming." << endl;

}

4

2 回答 2

2

你可以用循环替换ifwhile

cout << "Enter your selection (1, 2, 3 or 4): ";
while (menu)
{
    cin >> choice;
    menu = false;

    switch (choice)
    {
    case 1:
        cout << "You have chosen play";
        break;
    ....
    default:
        cout<< "Your selection must be between 1 and 4!\n";
        menu = true; // incorrect input, run loop again
}
于 2013-10-10T07:33:35.903 回答
1

你可以使用这样的do while循环:

int main() 
{
int choice;
bool menu = true;
do{
cout <<"Please select one of the following options:  \n";

cout << "1: Play\n"
    "2: Help\n"
    "3: Config\n"
    "4: Quit\n";

cout << "Enter your selection (1, 2,3 or 4): ";
cin >> choice;
//*****************************************************************************
// Switch menu to display the menu.
//*****************************************************************************
    switch (choice)
 {
      case 1:
        cout << "You have chosen play";
       int missed = playgame('programming');
        break;
     case 2:
        cout << "You have chosen help\n";
        cout << "Here is a description of the game Hangman and how it is    played:\nThe      word to guess is represented by a row of dashes, giving the number of letters, numbers and category. If the guessing player suggests a letter or number which occurs in the word, the other player writes it in all its correct positions";
        break; 
     case 3:
        cout << "You have chosen config";
        break;
         case 4:
        cout << "You have chosen Quit, Goodbye.";
        break;
    default:
        cout<< "Your selection must be between 1 and 4!\n";

    }

}while(choice!=4);    
getchar();
getchar();


cout << "You missed " << playGame("programming");
cout << " times to guess the word programming." << endl;

}

我在显示菜单之前添加了 do 循环,这样每次用户选择一个选项时,用户都会再次显示菜单,除非用户输入 4,在这种情况下,程序会退出 do while 循环。您可以在案例 1 中调用函数 playgame() ..我已经编辑了我的答案以包含 playgame();

于 2013-10-10T07:39:34.683 回答