So I wrote a super simple text based game with C++, and everything works until the end. My final if else statement is supposed to say either YOU WIN!!!, or you lose, instead it writes out both and I can't seem to figure out why.
#include <iostream>
usingnamespace std;
int main()
{
cout << "You are in a rooms containing 3 chests and a locked door" << endl;
cout << "Choose a chest, one of them contains a key" << endl;
cout << "Choose chest a, b or c" << endl;
char a;
cin >> a;
bool b;
switch(a)
{
case'a':
cout << "the key is not in this chest" << endl;
break;
case'b':
cout << "the key is not in this chest" << endl;
break;
case'c':
cout << "the key is in this chest, you have taken the key!" << endl;
break;
}
if (a == 'c')
{
bool b=true;
}
elseif (a == 'a')
{
b=false;
}
else (a == 'b');
{
b=false;
}
if ( b=true)
cout << "you have the key, and have entered the next room" << endl;
cout << "in this room there are two chests, chest a and chest b, and a locked door, there is a key in oune of the chests" << endl;
cout << "Choose a chest, a or b." << endl;
char c;
bool g;
cin >> c;
if (c == 'a')
{
g=false;
cout << "The key was not in this chest, try again" << endl;
}
elseif (c == 'b')
{
g=true;
cout << "the key was in this chest, you have taken the key" << endl;
}
if (g=true)
{
cout << "You have opened the next door" << endl;
cout << "In this roome, there is a trophy saying:" << endl;
cout << "YOU WIN!!!" << endl;
}
else (g=false);
{
cout << "You lost" << endl;
}
return 0;
}
Here's the output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
You are in a rooms containing 3 chests and a locked door
Choose a chest, one of them contains a key
Choose chest a, b or c
c
the key is in this chest, you have taken the key!
you have the key, and have entered the next room
in this room there are two chests, chest a and chest b, and a locked door, there is a key in oune of the chests
Choose a chest, a or b.
b
the key was in this chest, you have taken the key
You have opened the next door
In this roome, there is a trophy saying:
YOU WIN!!!
You lost
levi@levi-L
Oh, that explains why its was still giving me the error, the same thing was happening (g=false), that's why getting rid of it altogether worked. if g is true,else it's false I didn't need to add it at all.