我如何制定结束程序的条件,并防止进一步的 cout 语句?

How do i make a condition that will end the program, and prevent further cout statements?

我目前正在制作一个基于文本的游戏,只是为了好玩,它为用户提供了三种选择。其中一个选项允许用户继续,而其他选项则以 "GAME OVER" 文本结束游戏。我认为创建一个 endGame() 函数就可以解决问题,但终端仍然输出其余的 cout 语句。如何让 endGame() 函数结束程序? (程序还没有完成,我还打算增加很多功能。我只是在解决这个问题之前无法继续。)

此外,有没有一种方法可以确认用户为每个问题输入了 1、2 或 3,并且为错误的答案输入了 return "Enter 1, 2 or 3!",而不必将其编码到每个问题中问题?

#include <iostream>
#include <string>

using namespace std;

void endGame (void)
{
    cout << "GAME OVER\n";
}

int main() 
{
    string userName;
    //int choice1;
    //int choice2;
    //int choice3;
    int event1;
    int event2;
    int event3;
    int event4;
    int event5;

    cout << "What is your name, traveler?" << endl;
    cin >> userName;

    cout << "\nWelcome, " << userName << "." << endl;
    cout << "I'm going to ask you a series of questions." << endl;
    cout << "To cotinue your adventure, answer them by typing" << endl;
    cout << "either 1, 2, or 3." << endl;

    cout << "\nYou're walking down a long dirt road. You hear footsteps behind you," << endl;
    cout << "but you're too afraid to look right now. A fork in the road lies infront of you." << endl;

    cout << "\nWhat do you do?" << endl;
    cout << "1 - Turn left, and run down the hill." << endl;
    cout << "2 - Turn right, and run towards the abandoned house in the distance." << endl;
    cout << "3 - Gain the courage to turn around and face whatever is behind you." << endl;
    cin >> event1;
    if(event1 == 1)
    {
        cout << "Unfortunatley, you trip over a branch, and fall to your death.\n" << endl;
        endGame();
    }
    else if(event1 == 2)
    {
        cout << "You run towards the house, and escape the creature following you." << endl;
    }
    else if(event1 == 3)
    {
        cout << "You turn around, but before you can make out the features of the creature behind you, it kills you.\n" << endl;
        endGame();
    }   

    cout << "As you get closer to the house, you notice a light is on upstairs." << endl;
    cout << "A small path leads to the back of the house.\n" << endl;
    cout << "What do you do?" << endl;
    cout << "1 - Knock on the door, and hope whoever is inside won't kill you." << endl;
    cout << "2 - Enter the hoouse unanounced, and sneak upstairs." << endl;
    cout << "3 - Follow the path to the back of the house." << endl;
    cin >> event2;
    if(event2 == 1)
    {
        cout << "A drunk, confused man answers the door, shotgun in hand." << endl;
        cout << "You raise your hands in defense, but before you can explain yourself, he shoots." << endl;
        endGame();
    }
    else if(event2 == 2)
    {
        cout << "You slowly open the door, and quietly sneak upstairs." << endl;
    }
    else if(event2 == 3)
    {
        cout << "You sneak around the house, and become face to face with a vicious gaurd dog." << endl;
        cout << "Before you can give it a Scooby-Snac, it bits your throat, and kills you." << endl;
        endGame();
    }
    cout << "You see three doorways, one leads to a dimly lit room, the second to a brightly lit room, and the third to a pitch black room." << endl;
    cout << "\nWhat do you do?" << endl;

    return 0;
}
#include <iostream>
#include <string>
using namespace std;

int main() 
{
    string userName;
    //int choice1;
    //int choice2;
    //int choice3;
    int event1;
    int event2;
    int event3;
    int event4;
    int event5;

    int gameOver = 0;

    cout << "What is your name, traveler?" << endl;
    cin >> userName;

    cout << "\nWelcome, " << userName << "." << endl;
    cout << "I'm going to ask you a series of questions." << endl;
    cout << "To cotinue your adventure, answer them by typing" << endl;
    cout << "either 1, 2, or 3." << endl;

    cout << "\nYou're walking down a long dirt road. You hear footsteps behind you," << endl;
    cout << "but you're too afraid to look right now. A fork in the road lies infront of you." << endl;

    cout << "\nWhat do you do?" << endl;
    cout << "1 - Turn left, and run down the hill." << endl;
    cout << "2 - Turn right, and run towards the abandoned house in the distance." << endl;
    cout << "3 - Gain the courage to turn around and face whatever is behind you." << endl;
    cin >> event1;
    if(event1 == 1)
    {
        cout << "Unfortunatley, you trip over a branch, and fall to your death.\n" << endl;
        gameOver = 1;
    }
    else if(event1 == 2)
    {
        cout << "You run towards the house, and escape the creature following you." << endl;
    }
    else if(event1 == 3)
    {
        cout << "You turn around, but before you can make out the features of the creature behind you, it kills you.\n" << endl;
        gameOver = 1;
    }   

    if (!gameOver)
    {
        cout << "As you get closer to the house, you notice a light is on upstairs." << endl;
        cout << "A small path leads to the back of the house.\n" << endl;
        cout << "What do you do?" << endl;
        cout << "1 - Knock on the door, and hope whoever is inside won't kill you." << endl;
        cout << "2 - Enter the hoouse unanounced, and sneak upstairs." << endl;
        cout << "3 - Follow the path to the back of the house." << endl;
        cin >> event2;
        if(event2 == 1)
        {
            cout << "A drunk, confused man answers the door, shotgun in hand." << endl;
            cout << "You raise your hands in defense, but before you can explain yourself, he shoots." << endl;
            gameOver = 1;
        }
        else if(event2 == 2)
        {
            cout << "You slowly open the door, and quietly sneak upstairs." << endl;
        }
        else if(event2 == 3)
        {
            cout << "You sneak around the house, and become face to face with a vicious gaurd dog." << endl;
            cout << "Before you can give it a Scooby-Snac, it bits your throat, and kills you." << endl;
            gameOver = 1;
        }

        if (!gameOver)
        {
            cout << "You see three doorways, one leads to a dimly lit room, the second to a brightly lit room, and the third to a pitch black room." << endl;
            cout << "\nWhat do you do?" << endl;
        }
    }
}

基本上就是把游戏是否结束存入gameOver。不要调用 gameOver(),只需将这个新的 gameOver 变量设置为 1(这意味着 true)。然后,稍后,您可以检查游戏是否结束,如果结束则跳过其他所有内容。

我建议您使用 switch case 作为条件,使用 while 循环作为输入 1、2、3 的东西。类似

bool running=true;
while(running)
{ 
    running = false;
    switch(i)
    { 
        case 1: stuff; break;
        case 2: stuff; break;
        case 3: stuff; break;
        default: cout<<"enter 1,2,3"; running = true;
    }
}

对于残局的事情,使残局功能变得如此

void endgame()
{
    cout<<"game over";
    system("PAUSE");
    exit(0);
}