首页 > 解决方案 > If 语句问题 [C++]

问题描述

所以一切正常,您可以为选择字符串执行第一个 cin,但在那之后,if 语句中的choice2 的 cin 不起作用。该程序将结束。我对 C++ 很陌生,我真的需要一些指针来变得更流利。请帮我解决一下这个。

#include <iostream>
using namespace std;

int main() {
   
    string Choice;
    string Choice2;
    string Choice3;
    string Choice4;
   
    cout << "Welcome to the Police Adventure game! A text based adventure game where you can take on the life of a police officer choosing the best choice for the current situation!" << endl;
    cout <<  "You start your career and pull someone over. Please type either A, or B. Do you (A)Approach cautiously and ask them for their ID, or do you (B)Approach quickly with your gun drawn in hopes that they are wanted." << endl;
   
    cin >> Choice;
   
    if (Choice == "A") {
       
        cout << "The driver hands their ID to you, and you head back to your vehicle to run it through the computer." << endl;
        cout << "Please type either A, or B. Do you (A)Run the ID, or do you (B)Pretend to run the ID." << endl;
        cin >> Choice2;
       
    }
    else if (Choice2 == "A") {
       
        cout << "The driver comes back as a wanted felon with one charge of homicide." << endl;
        cout << "Please type either A, or B. Do you (A)Order them out of the vehicle at gunpoint, or do you (B)Pretend to not notice it and let them go." << endl;
        cin >> Choice3;
       
    }
    else if (Choice2 == "B") {
       
        cout << "You pretend to run the ID, and let the suspect go. It turns out that they were wanted for homicide, and killed an officer right before he was caught." << endl;
        cout << "GAME OVER" << endl;
       
    }
    else if (Choice3 == "A") {
       
        cout << "The driver tells you he has a gun, and throws it out of the window. The driver then proceeds to slowly exit the vehicle." << endl;
        cout << "Please type either A, or B. Do you (A)Arrest them, or do you (B)Shoot them." << endl;
        cin >> Choice4;
   
    }
    else if (Choice4 == "A") {
       
        cout << "You arrested the driver, and got a promotion." << endl;
        cout << "YOU WIN" << endl;
       
    }
    else if (Choice4 == "B") {
       
        cout << "You shot the driver dead, and you were fired and arrested for shooting a non-deadly suspect." << endl;
        cout << "GAME OVER" << endl;
       
    }
    if (Choice == "B") {
       
        cout << "The driver grabs a concealed weapon and opens fire killing you and one civilian in the crossfire." << endl;
        cout << "GAME OVER" << endl;
       
    }
}

标签: c++if-statement

解决方案


你的游戏结构全错了。单个语句序列if..else将不起作用。您需要多个嵌套 if序列。

尝试更多类似的东西:

#include <iostream>
#include <string>
using namespace std;

int main() {
    string Choice;

    cout << "Welcome to the Police Adventure game! A text based adventure game where you can take on the life of a police officer choosing the best choice for the current situation!" << endl;

    cout << "You start your career and pull someone over. Please type either A, or B. Do you (A)Approach cautiously and ask them for their ID, or do you (B)Approach quickly with your gun drawn in hopes that they are wanted." << endl;

    cin >> Choice;

    if (Choice == "B") {
        cout << "The driver grabs a concealed weapon and opens fire killing you and one civilian in the crossfire." << endl;
        cout << "GAME OVER" << endl;
    }
    else if (Choice == "A") {
        cout << "The driver hands their ID to you, and you head back to your vehicle to run it through the computer." << endl;
        cout << "Please type either A, or B. Do you (A)Run the ID, or do you (B)Pretend to run the ID." << endl;

        cin >> Choice;

        if (Choice == "B") {
            cout << "You pretend to run the ID, and let the suspect go. It turns out that they were wanted for homicide, and killed an officer right before he was caught." << endl;
            cout << "GAME OVER" << endl;
        }
        else if (Choice == "A") {
            cout << "The driver comes back as a wanted felon with one charge of homicide." << endl;
            cout << "Please type either A, or B. Do you (A)Order them out of the vehicle at gunpoint, or do you (B)Pretend to not notice it and let them go." << endl;

            cin >> Choice;

            if (Choice == "B") {
                cout << "You were fired for not arresting a deadly suspect." << endl;
                cout << "GAME OVER" << endl;
            }
            else if (Choice == "A") {
                cout << "The driver tells you he has a gun, and throws it out of the window. The driver then proceeds to slowly exit the vehicle." << endl;
                cout << "Please type either A, or B. Do you (A)Arrest them, or do you (B)Shoot them." << endl;

                cin >> Choice;

                if (Choice == "A") {
                    cout << "You arrested the driver, and got a promotion." << endl;
                    cout << "YOU WIN" << endl;
                }
                else if (Choice == "B") {
                    cout << "You shot the driver dead, and you were fired and arrested for shooting a non-deadly suspect." << endl;
                    cout << "GAME OVER" << endl;
                }
            }
        }
    }

    return 0;
}

然后您可以进一步简化:

#include <iostream>
#include <string>
using namespace std;

int main() {
    string Choice;

    cout << "Welcome to the Police Adventure game! A text based adventure game where you can take on the life of a police officer choosing the best choice for the current situation!" << endl;

    cout << "You start your career and pull someone over. Please type either A, or B. Do you (A)Approach cautiously and ask them for their ID, or do you (B)Approach quickly with your gun drawn in hopes that they are wanted." << endl;

    cin >> Choice;

    if (Choice == "B") {
        cout << "The driver grabs a concealed weapon and opens fire killing you and one civilian in the crossfire." << endl;
        cout << "GAME OVER" << endl;
        return 0;
    }

    if (Choice != "A") {
        cout << "That is not a valid choice." << endl;
        cout << "GAME OVER" << endl;
        return 0;
    }

    cout << "The driver hands their ID to you, and you head back to your vehicle to run it through the computer." << endl;
    cout << "Please type either A, or B. Do you (A)Run the ID, or do you (B)Pretend to run the ID." << endl;

    cin >> Choice;

    if (Choice == "B") {
        cout << "You pretend to run the ID, and let the suspect go. It turns out that they were wanted for homicide, and killed an officer right before he was caught." << endl;
        cout << "GAME OVER" << endl;
        return 0;
    }

    if (Choice != "A") {
        cout << "That is not a valid choice." << endl;
        cout << "GAME OVER" << endl;
        return 0;
    }

    cout << "The driver comes back as a wanted felon with one charge of homicide." << endl;
    cout << "Please type either A, or B. Do you (A)Order them out of the vehicle at gunpoint, or do you (B)Pretend to not notice it and let them go." << endl;

    cin >> Choice;

    if (Choice == "B") {
        cout << "You were fired for not arresting a deadly suspect." << endl;
        cout << "GAME OVER" << endl;
        return 0;
    }

    if (Choice != "A") {
        cout << "That is not a valid choice." << endl;
        cout << "GAME OVER" << endl;
        return 0;
    }

    cout << "The driver tells you he has a gun, and throws it out of the window. The driver then proceeds to slowly exit the vehicle." << endl;
    cout << "Please type either A, or B. Do you (A)Arrest them, or do you (B)Shoot them." << endl;

    cin >> Choice;

    if (Choice == "A") {
        cout << "You arrested the driver, and got a promotion." << endl;
        cout << "YOU WIN" << endl;
    }
    else if (Choice == "B") {
        cout << "You shot the driver dead, and you were fired and arrested for shooting a non-deadly suspect." << endl;
        cout << "GAME OVER" << endl;
    }
    else {
        cout << "That is not a valid choice." << endl;
        cout << "GAME OVER" << endl;
    }

    return 0;
}

推荐阅读