Hey homies, Im having game entrance trouble. Where it says 'Press X' if you press any other letter it skips to the end of the game... How can I keep that from happening?
Thanks
-Quinn
#include <iostream>
#include <string>
using namespace std;
cout << " Please choose either A - B - C to answer the question" << endl;
cout << " ...Good luck..." << endl;
cout << " " << endl;
cout << "________________________________________________________________________________" << endl;
cout << " " << endl;
cout << " " << endl;
cout << " " << endl;
cout << " " << endl;
cout << " - Press 'X' to Begin Game - " << endl;
string answer;
cin >> answer;
if (answer == "x")
{
cout << " " << endl;
cout << "It is the year 2110. You are alone in your home, late at night." << endl;
cout << "You are sitting in bed, on your iPhone 23 listening to music with your AirPods" << endl;
cout << "All of a sudden comes a huge BANG! on the door...the noise continues" << endl;
cout << "You look out the little window at the top, praying for it to be your friend, Joe" << endl;
cout << "Thank God, you think, it is Joe. He doesn't look right. He's pale..." << endl;
cout << "What do you do? A - Open Door B - Call for help";
string answer;
cin >> answer;
if (answer == "a")
{
cout << "Joe hits you with a shovel, right over the head.";
cout << "GAME OVER" << endl;
}
else {
cout << "_________________________________________________________________________________" << endl;
cout << "You call for help as you go to the basement and lock all the doors behind you...uh oh" << endl;
cout << "Your mom picks up the phone, you tell her to get to the house immediatley" << endl;
cout << "Meanwhile, more and more banging comes from upstairs..." << endl;
cout << "Do you A - Run out back door or B - Stay put" << endl;
}
answer;
cin >> answer;
if (answer == "b")
{
cout << "Joe enters, looking for you. You scream, black out. Your dead." << endl;
cout << "GAME OVER" << endl;
}
else {
cout << "Yo man! Joe yells at you, he begins running after you!" << endl;
cout << "What do you do?" << endl;
cout << "Do you A - Stop and speak with Joe or B - Run away to your mom who just pulled in";
}
answer;
cin >> answer;
if (answer == "A")
{
cout << "Joe stabs you with a machetti; it's over..." << endl;
cout << "GAME OVER" << endl;
}
else{
cout << "MOM! Whats going on around this place?!" << endl;
cout << "Idk honey, havent been here for a few hours...What happened?" << endl;
cout << "I think someone is haunting, my friend Joe? He is acting super odd" << endl;
cout << "In what way? Joe is a good kid!" << endl;
cout << "Well, mom, he had a knife. He scared me so bad I ran out here..." << endl;
cout << "Alright honey, what do you suppose we do?" << endl;
cout << "A - Leave to find your father B - Investigate C - Call or Help " << endl;
}
answer;
cin >> answer;
if (answer == "b")
{
cout << "You get out of the car, and investigate for Joe..." << endl;
cout << "Your mother goes right and you go left" << endl;
cout << "You hear a shreik and run to the east side of the home" << endl;
cout << "There is your mother, a hole in her chest, dead from a gunshot wound" << endl;
cout << "You search for Joe, ready to kill him now!" << endl;
cout << "Then, you see him, you let him know he is about to die" << endl;
cout << "Then, you realize you are the smartest person ever: No Weapon!" << endl;
cout << "Immediatley Joe rushes you, stabs you in the heart." << endl;
cout << "GAME OVER" << endl;
}
else if (answer == "a")
{
cout << "You begin driving to find your dad. Where could he be?" << endl;
cout << "You decide to call him, but as you pick up your phone, someone else calls..." << endl;
cout << "It reads 'Joeey. You answer...:" << endl;
cout << "Hey Joe, how ya feelin?" << endl;
cout << "DEATH! DEATH! DEATH!" << endl;
cout << "Immediatley you know now that Joe is a dangerous man.." << endl;
cout << "CRASH! The car window breaks with a smash, you see a weilded knife!";
cout << "Joe stabs your mother, and then you, you bleed out and then, die." << endl;
cout << "GAME OVER" << endl;
}
else{
cout << "You drive off to a nearby WalMart to stay safe" << endl;
cout << "You lookup the local police stations number and call them" << endl;
cout << "You let them know and ask them to investigate the suspucious activity." << endl;
cout << "Now you must decide, do you A - go to the house and wait for police?" << endl;
cout << "or B - Hang tight until help arrives at your home?" << endl;
}
answer;
cin >> answer;
if (answer == "a")
{
cout << "You drive home and hope that you'll be safe..." << endl;
cout << "You show up, seeing that the police have arrived..." << endl;
cout << "Great, you think, I'm safe...but no" << endl;
cout << "Thats not the police, thats Joe...and a friend..." << endl;
cout << "His friend walks up to you and you try to drive but you can't." << endl;
cout << "He takes a Revolver 46x Sniper 49 Roll Tide to your face.." << endl;
cout << "He shoots you between the eyes..." << endl;
cout << "GAME OVER" << endl;
}
}
else{
cout << "You wait at WalMart for a call from the police," << endl;
cout << "They finally call, you pick up the phone..you hear Joe's voice.." << endl;
cout << "DEATH DEATH DEATH! screams Joe.." << endl;
cout << "You scream so loud that a WalMart attendant comes to see you..." << endl;
cout << "Like most WalMateers she is a solid 415lbs and 65 years old..." << endl;
cout << "She comes out on an electric cart and asks why you screamed..." << endl;
cout << "You explain to her that your grandmother just died, since you shouldn't tell anyone.." << endl;
cout << "She pops a Machetti out from under her cart..uh oh" << endl;
cout << "She takes it to your stomach, and stabs you hard...you die..." << endl;
cout << " " << endl;
cout << " " << endl;
cout << " " << endl;
cout << " " << endl;
cout << " " << endl;
cout << " " << endl;
cout << "- GAME OVER - " << endl;
cout << "Don't worry, you picked the right answer, but this game had to end somewhere " << endl;
cout << "I hope it was a good run, and congrats on winning!" << endl;
cout << "Please give Feedback and I'll be making more games soon!" << endl;
cout << "Thanks - Quinn" << endl;
}
}
do{
cout<<"Press x to start the game : ";
cin>>answer;
}while(answer!='x' || answer!='X');
A suggestion, declare your variables at the beginning of main, its not necessary but it's good practice. Also, since answer will always hold one character, declaring it as string is not necessary, use char instead. Good luck
What is the purpose of the "answer;"? Answer is also declared multiple times.
1 2 3
cin >> answer;
if (answer == "x")
{ ...
In answer to your question, the reason it skips to the end of the game is because you have put the entire game into the if statement above, this means that when answer is not equal to X it skips the whole thing. To stop seeing the end of the game make sure you have the correct closing braces, I think you have an extra one here:
1 2 3 4 5
cout << "GAME OVER" << endl;
}
}
else{
cout << "You wait at WalMart for a call from the police," << endl;
It seems like you may have copied and pasted a lot of the code and this has caused some errors, such as multiple closing brackets and multiple declarations.