cout commands not displaying

hi! i'm writing a game but when i compile and run the program the cout commands in the if and else if statements do not show. what's wrong with the code?

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
cout<<"what next?\n";
cout<<"1. arm your civilization.\n";
cout<<"2. build farms on your planet.\n";
cout<<"pick a number: ";
cin>>a;

if (a==1&&z==1){
cout<<"you have created religious fundamentalists, with your as their God!\n";
cout<<"you receive five bonus points.\n";
a=a+5;
}
else if (a==2&&z==1){
cout<<"you have created a peaceful, united civilization.\n";
cout<<"you have earned two bonus points.\n";
a=a+2;
}
else if (a==1&&z==2){
cout<<"you have created a strong military force.\n";
cout<<"YOU HAVE ACHIEVED WORLD PEACE!!!\n";
cout<<"you have earned ten bonus points.\n";
a=a+10;
}
else if (a==2&&z==2){
cout<<"you have created warring tribes.\n";
cout<<"mission failed.";
cout<<"your TOTAL score: "<<x+y+z<<endl;
return 0;
}

cout<<"your score: "<<x+y+z+a<<endl;

What are the 'a' and 'z' variables? You are reading in 'a' using cin, and then writing a new value to it (in your if blocks) as if you are intending to keep the value around longer.

Is 'z' also being updated elsewhere in your code? Can you be sure that the only two possible values for 'z' are 1 and 2?

The only possible scenarios where anything other than your score will print are when the values for 'a' and 'z' match the table below:

a z value of 'a' (after if block)
1 1 6
2 1 4
1 2 11
2 2 2

the a and z variables are cin inputs that refer to choices made by the user.
the choices are either 1 or 2, and the point is to follow a path relating to which of the choices the user makes through the game.

earlier they make a cin>>z input, which is either 1 or 2, and I am trying to make it so that if the user chooses 1 at z, and 1 at a, the user will receive a message:
cout<<"you have created religious fundamentalists, with your as their God!\n";
cout<<"you receive five bonus points.\n";
but it's not happening. the only thing that happens is that the cout messages are skipped and the points counted and illustrated under:
cout<<"your score: "<<x+y+z+a<<endl;
where x and y are two previous cin inputs. the addition of the score works but the cout messages are not showing up in the program.
Last edited on
Either use a debugger to track the values of variables, and trace the execution line by line, or add additional messages to help diagnose what is happening. For example, just after line 6, insert this:

cout << "a = " << a << " z = " << z << endl;

Also how are a and z declared? Are they type double or int or char or string or what exactly?
Are you assigning another value to 'z' inside your if blocks as you are doing with 'a'? If so, then the value for z entered by the user is no longer available and your if/else checks will never find a match.

Also, I assume a and z are both integer values, and not char or some other type?

One way to find out what is going on would be to print the values of 'a' and 'z' just before your if/else block.

1
2
3
4
cout << "a = " << a << "  and z = " << z << endl;

if ( a==1 && z==1) { ... }
...


That should immediately show you if either 'a' or 'z' aren't what you were expecting to see, and you can then track down where the problem might be. I suspect the problem is with the value of z.

the full script is:

#include <iostream>
using namespace std;

int main ()
{
int x;
int y;
int z;
int a;
int b;
int c;

//introduction

cout << "Hello. Welcome to Planets of Evolution!\n";

cout << "Choose your planet: \n";
cout << "1. Venus\n";
cout << "2. Earth\n";
cout << "3. Mars\n";
cout << "4. Jupiter\n";
cout << "5. Pluto\n";
cout << "Type its number and then press Enter: ";
cin >> x;

if (x==1){
cout << "Welcome Venetians! You are God of your planet. You must design ";
cout << "a species that will conquer the solar system.\n";
}
else if (x==2){
cout << "Welcome Earthlings! You are God of your planet. You must ";
cout << "design a species that will conquer the solar system.\n";
}
else if (x==3){
cout << "Welcome Martians! You are God of your planet. You must ";
cout << "design a species that will conquer the solar system.\n";
}
else if (x==4){
cout << "Welcome Ionions! You are God of your planet. You must ";
cout << "design a species that will conquer the solar system.\n";
}
else if (x==5){
cout << "Welcome Plutonians! You are God of your planet. You must ";
cout << "design a species that will conquer the solar system.\n";
}
else {
cout << "Planet not recognised. \n";
return 0;
}
cout <<"Your points: " << x << '\n';


cout <<"Let's Get Ready to EVOLVE!!\n";

//new stage (2)

cout <<"Upon finding a promising primitive race, you make your first ";
cout <<"decision. Choose:\n";
cout <<"1. kill\n";
cout <<"2. give love\n";
cout <<"3. give technology\n";

cout <<"pick a number: ";
cin >> y;

if (y==1){
cout<<"you've killed your only promising species.\n";
cout <<"better luck next time.\n";
return 0;
}
else if (y==2){
cout<<"you have provided warmth to your civilization.\n";
cout <<"your civilization nestles under fire.\n";
}
else if (y==3){
cout<<"you have provided means to escape life's hardships.\n";
cout << "your civilization is at peace.\n";
}
else {
cout <<"your civilization is confused and arms itself to kill you off.\n";
cout<<"game over.\n";
return 0;
}

cout <<"your points: " << x+y << endl;

cout << "your planet shows signs of intelligence. what next?\n";
cout <<"1. build them a temple.\n";
cout <<"2. build a castle for yourself.\n";
cout <<"pick a number: ";
cin >> z;

if (z==1){
cout<<"your civilization is united in prayer of you.\n";
cout<<"you receive two bonus points.\n";
z=z+2;
}
else if (z==2){
cout<<"your civilization seeks shelter in your castle.\n";
cout<<"you receive two bonus points.\n";
z=z+2;
}
else{
cout<<"what you doin'?";
return 0;
}
cout<<"your points: "<<x+y<<endl;

//new stage
cout<<"what next?\n";
cout<<"1. arm your civilization.\n";
cout<<"2. build farms on your planet.\n";
cout<<"pick a number: ";
cin>>a;

if (a==1&&z==1){
cout<<"you have created religious fundamentalists, with your as their God!\n";
cout<<"you receive five bonus points.\n";
a=a+5;
}
else if (a==2&&z==1){
cout<<"you have created a peaceful, united civilization.\n";
cout<<"you have earned two bonus points.\n";
a=a+2;
}
else if (a==1&&z==2){
cout<<"you have created a strong military force.\n";
cout<<"YOU HAVE ACHIEVED WORLD PEACE!!!\n";
cout<<"you have earned ten bonus points.\n";
a=a+10;
}
else if (a==2&&z==2){
cout<<"you have created warring tribes.\n";
cout<<"mission failed.";
cout<<"your TOTAL score: "<<x+y+z<<endl;
return 0;
}

cout<<"your score: "<<x+y+z+a<<endl;

Last edited on
You've put the line to output a before the user has entered a value for it, so it'll have some uninitialized garbage value in it.
ok so z switches to 4 at this point:

Hello. Welcome to Planets of Evolution!
Choose your planet:
1. Venus
2. Earth
3. Mars
4. Jupiter
5. Pluto
Type its number and then press Enter: 4
Welcome Ionions! You are God of your planet. You must design a species that will conquer the solar system.
Your points: 4
Let's Get Ready to EVOLVE!!
Upon finding a promising primitive race, you make your first decision. Choose:
1. kill
2. give love
3. give technology
pick a number: 3
you have provided means to escape life's hardships.
your civilization is at peace.
your points: 7
your planet shows signs of intelligence. what next?
1. build them a temple.
2. build a castle for yourself.
pick a number: 2
your civilization seeks shelter in your castle.
you receive two bonus points.
your points: 7
what next?
1. arm your civilization.
2. build farms on your planet.
pick a number: 1
a = 1 and z = 4
your score: 12
what next?
1. provide advanced technology.
2. produce music.
pick a number:

after the code was placed:
cout<<"what next?\n";
cout<<"1. arm your civilization.\n";
cout<<"2. build farms on your planet.\n";
cout<<"pick a number: ";
cin>>a;

cout << "a = " << a << " and z = " << z << endl;

if (a==1&&z==1){

but why?

You add to z here - so if they entered 1 originally for z, it changes to 3. If they entered 2, it changes to 4. So when you get to comparing a and z, z no longer equals 1 or 2.

1
2
3
4
5
6
7
8
9
10
if (z==1){
cout<<"your civilization is united in prayer of you.\n";
cout<<"you receive two bonus points.\n";
z=z+2;
}
else if (z==2){
cout<<"your civilization seeks shelter in your castle.\n";
cout<<"you receive two bonus points.\n";
z=z+2;
}
yep! it's working!

thanks everyone!
Topic archived. No new replies allowed.