0

嘿,我正在用 C++ 制作游戏。这是大学的工作,简要说明不使用头文件,游戏必须是基本的。问题是,游戏结束后它仍然要求选择。我试图打破,退出,但仍然没有快乐。程序不会退出。谁能帮我这个?

这是我的代码:主要

int main()          //  The main function is set to int.  
    //  The return value has to be an integer value.
{
    menuText();
    while(menu)     // Loop to revert back to menu when choice is not compatable with options.
    {
        int selection;
        cout<< "Choice: ";
        cin>> selection;

        switch(selection)
        {
        case 1: 
            cout << "Start Game\n";
            playGame();
            break;
        case 2:
            cout << "Exit Game\n";
            cout << "Please press enter to exit...\n";
            menu = false ;
            break;
        }
    }


    system("pause");    //  To stop the program from exiting prematurely.
    return 0;           //  this is needed because the main is set to return
    //  an integer.
}

int playgame()
status Status = {100,20,80,80,20};// declaration of class members.
    //Contents of PlayGame().............................
    exitGame();
    return 0;
}
void exitGame()
{
    cout << "\n\nPlease press enter to exit the game.";
    return;

}
4

2 回答 2

2

你基本上这样做:

int playGame()
{
    return 0;
}

int main(){
    bool menu = true;
    while(menu){
        cout<< "Choice: ";
        cin>> selection;

        switch(selection)
        {
        case 1: 
            cout << "Start Game\n";
            playGame();
            break; // This break symbolizes that you want to end switch statement
                   // not the whole loop
        }
    }
    return 0;
}

你可以这样做:

#define GAME_COMPLETED -1 /* Will close the game */
#define GAME_RESERVER 0

if( playGame() == GAME_COMPLETED){
    menu = false;
}

// And of course at the end of the playGame:
return GAME_COMPLETED;

你确定你不想使用case '1':而不是case 1:吗?

于 2012-10-04T12:48:19.203 回答
1

我可能会误解你,但据我了解 - 你希望游戏在playGame()功能完成后结束(如果我错了,请纠正我)。

为此,您只需menu=false; 在此块中设置 - 或作为替代方案 -break;从这种情况下删除语句。它将使流程“下降”到出口案例。

于 2012-10-04T12:46:55.353 回答