ChickenEater
ChickenEater

Reputation: 1

If Statement Issues [C++]

So everything works, you can do the first cin for the choice string, but after that, the cin for choice2 in the if statement doesn't work. The program will just end. I am very new to C++ and I really need some pointers to get more fluent. Please help me with this.

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

Upvotes: 0

Views: 73

Answers (2)

Remy Lebeau
Remy Lebeau

Reputation: 598319

Your game structure is all wrong. A single sequence of if..else statements will not work. You need multiple nested if sequences.

Try something more like this:

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

Which you can then simplify a bit further:

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

Upvotes: 1

Albert
Albert

Reputation: 29

I guess you have the wrong level, you should read value before using it for Choice2, Choice3, Choice4.

You can try it.

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

Upvotes: 0

Related Questions