Small game problem, last bool variable seems to output both true and false

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.

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
#include <iostream>
using namespace 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;
		}
		else if (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;
		}
		else if (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
line 54 == not =
I tried that now, I'm still getting the same output, but that makes sense.
I got it, line 60 else (g=false); had to just be else. Thanks for the help it got me looking in the right spot.
(g = true) is assigning true to g, so that will always be true. You want (g == true) for a comparison.
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.
Topic archived. No new replies allowed.