Bool is setting itself to false

Hey guys. I've got this bit of code that is beginning to frustrate me so much and I just can't figure out why it isn't working the way I want it to.

I have a base class (Location) and I have 2 classes inheriting from it. "WaitingRoom" and "Arena". In WaitingRoom I've made a function that will set the bool variable I have in Location to TRUE. In Arena I have a function that just says --> If the bool is true, std::cout << "print this out";

I then call both of these functions (enterArena) and (isInArena) in a separate class called Game. I will copy this all out in order so it's easy to see.

Thanks heaps

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#pragma once
#include <iostream>
#include "myString.h"
class Location
{
public:
	Location();
	~Location();


	/*void getLocation();
	void arenaEntered();
	void waitingRoomEntered();
	void weaponsRoomEntered();
	void armorRoomEntered();
	void refreshmentsRoomEntered();*/

protected:
	bool inArena = false; //This is the bool messing up!
	bool inWaitingRoom = false;
	bool inWeaponsRoom = false;
	bool inArmorRoom = false;
	bool inRefreshmentsRoom = false;
};



1
2
3
4
5
6
7
8
9
10
void WaitingRoom::enterArena()
{
	string.newLine();
	std::cout << "Enter the arena and fight somebody by 
        typing 'goto arena'" << std::endl;
	if (string.compareStrings("goto arena") == 0)
	{
		Location::inArena = true; // At this point it's working, inArena is set to true
	}
}


1
2
3
4
5
6
7
8
void Arena::isInArena()
{
	while(Location::inArena == true) //Once it gets here, the inArena bool is set to false..
	{
		std::cout << "You are in the arena!" << std::endl;
		break;
	}
}


1
2
3
4
5
void Game::enterArena()
{
	waitingRoom.enterArena();
	arena.isInArena(); //Sometimes doesn't even enter this function
}


What's happening is, the inArena bool is setting to true but once it enters the isInArena function in the Arena class, the bool just automatically sets itself back to false, henceforth skipping everything inside that function.
Last edited on
You have apparently two variables 'waitingRoom' and 'arena'. What you do can be demonstrated with a simpler example:
1
2
3
4
5
6
7
8
9
int main() {
  bool foo = false;
  bool bar = false;

  foo = true; // foo is indeed true now

  if ( bar ) std::cout << "invisible";
  return 0;
}

Why should bar change when an unrelated variable foo does?

Class WaitingRoom object IS-A class Location object.
Class Arena object IS-A class Location object.
Two separate Location objects. Two objects with different type.
Hmm, not quite sure I understand exactly what you mean. So basically, even though
 
bool inArena = false; //LOCATION CLASS 

is in the location class, I can't play around with the variable from multiple inherited classes?

EDIT: Nevermind I understand completely now, gosh sometimes I feel like the biggest ditz when programming lol. Thanks heaps for the help!
Last edited on
By the way, I would discourage you from using Location::inArena etc, in your derived classes; I've never seen this before and I was confused as to how your code was even compiling before I saw that Arena was probably a derived class.

Also, your Location class probably shouldn't have booleans representing where the player is, the derived Location class itself should probably identify that information somewhere.
Also, your Location class probably shouldn't have booleans representing where the player is, the derived Location class itself should probably identify that information somewhere.

In derived? No.

In Location? Perhaps. Location could have single bool playerIsHere; What is wrong in player being in 13 locations simultaneously? Quantum theory does not oppose. Fellow with chainsaw does not oppose.


The game could have Location * currentLoc; that gets updated when the player moves.
Topic archived. No new replies allowed.