Only one choice working in choose your own adventure game.

I am making a "choose your own adventure game" and currently most of it is working. The game has two choices for the player to choose from. Whenever the correct choice is selected it prints the output but when the wrong choice is selected the program terminates and I can't figure out why. Anyone? Also, just ignore all the "std::", I put them in at one point and didn't feel like removing them.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
 #include "stdafx.h"
#include <iostream>
#include <string>


using namespace std;

int main(){
	int choice1;
	int choice2;
	int choice3;
	int age;
	string name;
	std::cout << "Hello! Before we begin allow me to ask a few questions." << std::endl;												// Pre-game
	std::cout << "How old are you?" << std::endl;
	cin >> age;
	if (age <= 12){
		std::cout << "Sorry, you are not old enough to play!" << std::endl;
	}
		else if (age >= 13){
			std::cout << "What is your name?" << std::endl;
			cin >> name;
			std::cout << "Hello " << name << ". Let's see how you do." << std::endl;
			std::cout << "Your walking through the forest and there is no one around and your phone is dead..." << std::endl;             // Start game
			std::cout << "Do you: " << std::endl;
			std::cout << "1: Look around" << std::endl;
			std::cout << "2: Stop and rest" << std::endl;
			cin >> choice1;
			if (choice1 == 1){                                                                                                               // Path_1: entry
				std::cout << "Out of the corner of your eye you spot him...Shia LaBeouf" << std::endl;
				std::cout << "He's following you about 30 feet back. He gets down on all fours and breaks into a sprint." << std::endl;
				std::cout << "Do you want to..." << std::endl;
				std::cout << "1: Stay and try to pet him" << std::endl;
				std::cout << "2: Run!" << std::endl;
				cin >> choice2;
				if (choice2 == 2){                                                                                                         // Path_2: Cont. 
					std::cout << "He's gaining on you. Shia Labeouf" << std::endl;
					std::cout << "Your looking for a car but you can't find one" << std::endl;
					std::cout << "1: Keep looking" << std::endl;
					std::cout << "2: Go back to running" << std::endl;
					cin >> choice3;
					if (choice3 == 2){																									// Path_3: entry
						std::cout << "Congratualtions, you guessed the right option." << std::endl;
						std::cout << "If you continue, things will become gruesome...so its your choice" << std::endl;
						std::cout << "Will you keep running from Shia LaBeouf? See all this happen in the sequel!...if I make one" << std::endl;	// The end															//Part_2 entry
					}
					else if (choice3 == 1){																							// Path_3: Fail_1
						std::cout << "Curious to see if you can find a car? How many times do people need to learn that 'curiosity killed the cat' also applies to humans" << std::endl;
					}
						else if (choice3 != 1 || choice3 != 2){                                                                                          //Input_Check_3
							std::cout << "Please ennter 1 or 2. No characters or other numbers" << std::endl;
						}
					else if (choice2 == 1){                                                                                                         // Path_2: Fail_1
						std::cout << "That was a mi-stake! haHaHA! Get it?! Because Shia LaBeouf is an actual cannibal, and your his dinner...and my creativity is lacking so my jokes are bad" << std::endl;
					}
			else if (choice2 != 1 || choice2 != 2){                                                                                               //Input_Check_2
					std::cout << "Please ennter 1 or 2. No characters or other numbers" << std::endl;
			}
		else if (choice1 == 2){                                                                                                            // Path_1: Fail_1
				std::cout << "You have just become a victim of Shia Labeouf" << std::endl;
		}
	else if (choice1 != 1 || choice1 != 2){																									//Input_Check_1
					std::cout << "Please ennter 1 or 2. No characters or other numbers" << std::endl;
	}
				}
			}
		}
}
Also, just ignore all the "std::", I put them in at one point and didn't feel like removing them.

Why would you remove them? It wouldn't work if you removed them.

when the wrong choice is selected the program terminates and I can't figure out why

Your code looks like it's meant to terminate if the wrong choice is entered. What did you think was going to happen if, for example, the user enters "17" for "choice1"?


Let's also look at some plain bad code. Let's look at this line:
if (choice1 != 1 || choice1 != 2)

The conditional statement here is:
(choice1 != 1 || choice1 != 2)

This is an OR statement, so if either side is TRUE, then the whole thing comes out as TRUE.

Let's take a look at the left side.
choice1 != 1 This will be FALSE only in one case, right? When choice1 is "1".

Let's take a look at the right side.
choice1 != 2 This will be FALSE only in one case, right? When choice1 is "2".

So for both sides to be FALSE, you need choice1 to be 1, and also 2, at the same time. Is that possible? No.

Basically, you've written a condition that is true always.
This is because you have no else statements or loops. If you want to force the user to pick from a table of choices and prevent the console from closing for one reason or the other, then use loops on that certain part and loop until the user picks a choice and have else statements.
This is an OR statement

Thanks for pointing that out, Moschops, I don't know why I had it as an OR.

It wouldn't work if you removed them.

It still works without them because I have using namespace std;

What did you think was going to happen if, for example, the user enters "17" for "choice1"?

A number like 17 would be handled by the now corrected
if (choice1 != 1 && choice1 != 2)
But my problem is for example in choice 1. the correct choice is 1 but if choice 2 is picked this is supposed to happen: cout << "That was a mi-stake! haHaHA! Get it?! Because Shia LaBeouf is an actual cannibal, and your his dinner...and my creativity is lacking so my jokes are bad" << endl;
But it does not print this. And what confuses me more is for choice 3 when the incorrect path is chosen, they get the correct final output of:
cout << "Curious to see if you can find a car? How many times do people need to learn that 'curiosity killed the cat' also applies to humans" << endl;
Last edited on
It still works without them because I have using namespace std;

That's what you should remove.


Line 53: else if (choice2 == 1){
The only way to get here is with choice2 set to 2 (line 36 enforces this). choice2 can never be 1 at line 53.
Last edited on
else if (choice3 == 1)

Thank you for this. Just to follow up I added a "}" between lines 52 & 53 and another between lines 58 & 59 and now it fully works.

That's what you should remove.

Also, why should I remove this? I just went through and removed all the "std::" while keeping using namespace std; and it works.
Because using namespace std; drags in an enormous number of classes and functions and increases the chances of name collision.

The whole point of namespaces is to prevent these collisions.
Topic archived. No new replies allowed.