bool issue

Dec 11, 2017 at 4:33pm
Why is my bool only coming out as whatever i define "chance" (ie, true or false) as at the end of my 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
bool CaptureAttempt(Monster monster)
{
	int chance;
	
	if (chance = 0)
	{
		if (monster.combatpower < 100)
		{
			chance = rand() % 1;
		}
		else if (monster.combatpower > 99 && monster.combatpower < 201)
		{
			chance = rand() % 3;
		}
		else if (monster.combatpower > 200)
		{
			chance = rand() % 7;
		}

		chance = true;
	}
	else
	{
		chance = false;
	}

	cout << chance << endl;
	
	return chance;
}
Dec 11, 2017 at 4:41pm
if (chance = 0) is not the same as if (chance == 0).
Dec 11, 2017 at 5:06pm
Furthermore, you have other errors as well.
(1) You never initialize chance, it will probably have some junk value in it, which is undefined behavior.
(2) do you want chance to be an int or a bool? You declare it as an int, but then assign it true, false, as well as other random numbers. It's a misleading to assign a boolean value to an int, because it will just be converted to 0 or 1 anyway.
Topic archived. No new replies allowed.