Проблемы с оператором If [C ++] - PullRequest
0 голосов
/ 04 августа 2020

Итак, все работает, вы можете выполнить первый cin для строки выбора, но после этого cin для выбора2 в операторе if не работает. Программа просто закончится. Я очень новичок в 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;
       
    }
}

Ответы [ 2 ]

1 голос
/ 04 августа 2020

Ваша игровая структура неверна. Одиночная последовательность операторов 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;
}
0 голосов
/ 04 августа 2020

Я думаю, у вас неправильный уровень, вы должны прочитать значение, прежде чем использовать его для Choice2, Choice3, Choice4.

Вы можете попробовать.

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

        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;

            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;

                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;
                }
            }
        }
        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 (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;
       
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...