Need help with a code

I am 14 years of age and have an interest in programming. I started this code to have a friendly game with my family for some laughs. Anyways, I started out pretty successful in my opinion. But after the 1st question the game wouldn't continue. It would just return? Here is the code


#include <iostream>

using namespace std;


int main()
{
int i;
int u;
int y;
int o;
cout << " Welcome to my game \n \n \n \n" << endl;

cout << "pick your character name" << endl;
cout << "Dan (enter 1)" << endl;
cout << "Briggs (enter 2)" << endl;
cout << "Cassidy (enter 3)" << endl;
cout << "Ann Marie (enter 4)" << endl;
cin >> i;
if(i==1){
cout << "Hello Dan, welcome to the game \n" << endl;
}
if(i==2){
cout << "Hello Briggs, welcome to the game \n" << endl;
}
if (i==3){
cout << "Hello Cassidy, welcome to the game \n" << endl;
}
if (i==4) {
cout << "Hello Ann Marie, welcome to the game \n" << endl;
}

cout << "We are going to play a game of WOULD YOU RATHER? \n \n" << endl;
cout << "How to play" << endl;
cout << "1. Read both scenarios" << endl;
cout << "2. Decide which you would rather do" <<endl;
cout << "3. If you prefer A, enter 1. If you prefer B, enter 2 \n \n" << endl;
cout << "Are you ready to play \n" << endl;
cout << "If you are ready, enter the magic number: 42" << endl;
cin >> u;
if(u==42){
cout << "Alright! Lets play!!! \n \n \n" << endl;
}

cout << "Would you rather?" << endl;
cout << "A. Have accordions for legs OR B. Have a belly button that is 10 inches long" << endl;
cin >> y;
if(y==2){
cout << "Correct! You can use it as a weapon! \n \n" << endl;

}
if (y==1){
cout << "Wrong, the belly button could've been used as a weapon! \n \n" << endl;
}
cout << "Here is another question \n \n" << endl;
cout << "Would you rather?" << endl;
cout << "A. Eat chocolate flavored poop? OR B. Eat poop flavored chocolate?" << endl;
int r;
cin >> r;
if(r==1){
cout << "Wrong never ever eat poop, you should know that by now" << endl;
}
if(r==2){
cout << "Correct, it's still chocolate, never eat poop!" << endl;
}



















return 0;
}



Thanks!
I don't see the problem you are talking about. It continues just fine. Anyways please use [‎code][/code‎‎‎] around you code. example [‎‎‏code]std::cout << "hello" << std::endl;[/code‎‎‎] looks like: std::cout << "hello" << std::endl;. I am curious as to why you use new variables each time. You can reuse variables. However, your variable names u, y, and r are questionable. You should name your variables with meaningful names.
Last edited on
I didn't know you could use the same variable each time, thanks for the response. Still learning :)
Another thing you should use an array for this:
1
2
3
4
5
6
7
8
9
10
11
12
if(i==1){
cout << "Hello Dan, welcome to the game \n" << endl;
}
if(i==2){
cout << "Hello Briggs, welcome to the game \n" << endl;
}
if (i==3){
cout << "Hello Cassidy, welcome to the game \n" << endl;
}
if (i==4) {
cout << "Hello Ann Marie, welcome to the game \n" << endl;
}


something like:

1
2
3
4
const int people = 4;
const std::string names[people] = {"Dan", "Briggs", "Cassidy", "Ann Marie"};

std::cout << "Hello " << names[i] << ", welcome to the game" << std::endl << std::endl;


Another thing you could do is replace that bit of code and this:
1
2
3
4
5
6
cout << "pick your character name" << endl;
cout << "Dan (enter 1)" << endl;
cout << "Briggs (enter 2)" << endl;
cout << "Cassidy (enter 3)" << endl;
cout << "Ann Marie (enter 4)" << endl;
cin >> i;


With something like:
1
2
3
4
5
std::string name;
std::cout << "Enter your character's name: ";
std::getline(std::cin, name);

std::cout << "Hello " << name << ", welcome to the game." << std::endl << std::endl;
Last edited on
I'll try it out, thanks for the help .
Topic archived. No new replies allowed.