Not sure why i'm exiting while loop

Although both my conditions are still true, for some reason i'm exiting my do while loop.

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
68
69
70
71
72
void Forest::enterForest(){
	int input = 0;
	bool choice = true;
	cout << "Now Entering: Deep Forest" << endl;
	cout << "Beware, not all levels have an exit" << endl;
	cout << "---------------------------------" << endl;
	do {
		cout << "Deep Forest Menu" << endl;	
		cout << "1. Go North" << endl;
		cout << "2. Go South" << endl;
		cout << "3. Go East"<< endl;
		cout << "4. Go West" << endl;
		cout << "5. Go North West" << endl;
		cout << "6. Go North East" << endl;
		cout << "7. Go South West" << endl;
		cout << "8. Go South East"<< endl;
		cout << "9. Cheat Sheet (temporary)" << endl;
		cout << "10. Show Location (temporary)" << endl;
		cout << "11. Show Stats" << endl;
		cout << "12. Click your heels" << endl;
		cout << "---------------------------------" << endl;
		cin >> input;
		switch (input){
		case 1:
			moveInForest("North");
			choice = true;
			break;
		case 2:
			moveInForest("South");
		choice = true;
			break;
		case 3:
			moveInForest("East");
			choice = true;
			break;
		case 4:
			moveInForest("West");
	choice = true;
			break;
		case 5:
			moveInForest("North West");
	choice = true;
			break;
		case 6:
			moveInForest("North East");
choice = true;
			break;
		case 7:moveInForest("South West");
	choice = true;
			break;
		case 8:
			moveInForest("South East");
		choice = true;
			break;
		case 9:
			displayForest();
	choice = true;
			break;
		case 10:
			showPlayerLocationInForest();
		choice = true;
			break;
		case 11:
			player->showCurrentStats();
			choice = true;
			break;
		case 12:
			input = 12;
			choice = false;
			break;
		}
	}while((choice == true) && (playerIsNotDead == true));


I look at my variable list while debugging and it shows that both choice and playerIsNotDead are both true an yet still exits the do while loop. What am I doing wrong?

Thanks :)

BTW I'm super newb to coding
Where exactly do you assign playerIsNotDead to be true ? I can't see it in the code you posted.
I didn't post it because I thought maybe I built the while loop wrong. Here is the code that is associated with playIsNotDead

this is in a file called subject.cpp

1
2
3
4
5
6
7
8
9
10
bool subject::checkIfAlive(){
	if (curHealth <= 0)
	{
		cout << "I'm sorry you've died!" << endl;
		cout << "Returning Home." << endl;
		location = 1;
		return false;
	}
	return true;
}


when a player moves in forest they have a chance for a mob to spawn and attack them (very primitive code so far)
this is in forest.cpp
1
2
3
4
5
6
7
8
9
void Forest::chanceToBattle(){
	int r = rand()% 3 + 1;
	if (r = 1){
		cout << "A Giant Wasp Attacks!" << endl;
		cout << "You hit it for 15 damage, killing it. You took 15 damage" << endl;
		player->takeDamage(15);
		playerIsNotDead = player->checkIfAlive();
	}
}


so playerIsNotDead is set equal to true if the player is not dead. Although when I run the code I get choice = true and playerIsNotDead = true(204)
in the locals
Hmmm. I can't seem to find the solution to your problem with a quick glance at your code, but I did noticed that line 3 of your forest.cpp file has a little mistake (probably a typo).

You're missing one = operator to your if.
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
68
69
70
71
72
void Forest::enterForest(){
	int input = 0;
	bool choice = true;
	cout << "Now Entering: Deep Forest" << endl;
	cout << "Beware, not all levels have an exit" << endl;
	cout << "---------------------------------" << endl;
	do {
		cout << "Deep Forest Menu" << endl;	
		cout << "1. Go North" << endl;
		cout << "2. Go South" << endl;
		cout << "3. Go East"<< endl;
		cout << "4. Go West" << endl;
		cout << "5. Go North West" << endl;
		cout << "6. Go North East" << endl;
		cout << "7. Go South West" << endl;
		cout << "8. Go South East"<< endl;
		cout << "9. Cheat Sheet (temporary)" << endl;
		cout << "10. Show Location (temporary)" << endl;
		cout << "11. Show Stats" << endl;
		cout << "12. Click your heels" << endl;
		cout << "---------------------------------" << endl;
		cin >> input;
		switch (input){
		case 1:
			moveInForest("North");
			choice = true;
			break;
		case 2:
			moveInForest("South");
		choice = true;
			break;
		case 3:
			moveInForest("East");
			choice = true;
			break;
		case 4:
			moveInForest("West");
	choice = true;
			break;
		case 5:
			moveInForest("North West");
	choice = true;
			break;
		case 6:
			moveInForest("North East");
choice = true;
			break;
		case 7:moveInForest("South West");
	choice = true;
			break;
		case 8:
			moveInForest("South East");
		choice = true;
			break;
		case 9:
			displayForest();
	choice = true;
			break;
		case 10:
			showPlayerLocationInForest();
		choice = true;
			break;
		case 11:
			player->showCurrentStats();
			choice = true;
			break;
		case 12:
			input = 12;
			choice = false;
			break;
		}while((choice == true) && (playerIsNotDead == true));
	}


see if this works I couldn't see much since I dont know your functions but at least it seems that your do...while is not correctly posted, its a logic error.
the while condition should be on the main brace, its right after the do...
Jonnhy: If I change the position of the brace I get an error expected while so I think it is formatted correctly.

Annatar: thanks for catching that for me!

I still haven't resolved this issue and am quite baffled because if they are both true it should continue the loop. But it doesn't.
I am nearly positive it has something to do with my bool being true(a number) rather then just true when I look at the locals. I can't find any forum postings about this, anyone know?
Topic archived. No new replies allowed.