I've been writing a crude Text Based adventure for practice, but I've run into this problem and I can't figure out how to fix the crashing. I'm afraid this problem is a bit above my skill level, so I'm going to try and get an answer here.
I'm going to try and only post the relevant things.
class Location{
public:
Location *ConnectionNorth,
*ConnectionSouth;
void ConnectedNorth(Location room){ConnectionNorth = &room;}
void ConnectedSouth(Location room){ConnectionSouth = &room;}
}
int main(){
Location roomA;
Location roomB;
Location *currentRoom;
roomA.ConnectedNorth(roomB);//room B is connected to A to the North
roomB.ConnectedSouth(roomA);//room A is connected to B to the South
currentRoom = &roomA;
while(playing){
//get input
if(input == wants to go north)
currentRoom = currentRoom->ConnectionNorth;
//tell user they're in room B
if(input == wants to go south)
currentRoom = currentRoom->ConnectionSouth;
//tell user they're in room A
}
The annoying part is my code works for room changes about 3 times before crashing, I assume it's because I'm doing something wrong with the pointers, but I lack the knowledge.
while(playing){
if(input == wants to go north)
currentRoom = currentRoom->ConnectionNorth;
}
The problem is in your while loop. With currentRoom pointing to roomA's ConnectionNorth variable, you are assigning roomB's address to the currentRoom pointer. If you select the "wants to go north option" again, you would be referencing roomB's ConnectionNorth variable which is not initialized.
You have a problem with pointers having un assigned and/or invalid values.
You initialized just 1 of the 2 pointers for each roomA and roomB.The other should be assigned NULL (or 0 or nullptr), and then this value tested for before using the pointer.
To prevent the crashing:
// A constructor for Location class Location(): ConnectionNorth(0), ConnectionSouth(0) {}
Then in main():
[code
if(input == wants to go north)
if( currentRoom ) currentRoom = currentRoom->ConnectionNorth;
//tell user they're in room B
if(input == wants to go south)
if( currentRoom ) currentRoom = currentRoom->ConnectionSouth;
//tell user they're in room A
][/code]
This will let you step off of the map and get stuck though. Can you think of a fix for that?
EDIT: Always correcting for keystrokes missed by my wireless keyboard.
class Location{
public:
Location *ConnectionNorth,
*ConnectionSouth,
*ConnectionEast,
*ConnectionWest;
void ConnectedNorth(Location* room){ConnectionNorth = room;}
void ConnectedSouth(Location* room){ConnectionSouth = room;}
void ConnectedEast(Location* room){ConnectionSouth = room;}
void ConnectedWest(Location* room){ConnectionSouth = room;}
}
int main(){
Location roomA;
Location roomB;
Location roomC;
Location *currentRoom;
roomA.ConnectedNorth(&roomB);//room B is connected to A to the North
roomB.ConnectedSouth(&roomA);//room A is connected to B to the South
roomB.ConnectedEast(&roomC);
roomC.ConnectedWest(&roomB);
currentRoom = &roomA;
/* B -- C
|
A */
while(playing){
//get input
if(input == wants to go north)
if(can go north)
currentRoom = currentRoom->ConnectionNorth;
else //say cant go
if(input == wants to go south)
if(can go south)
currentRoom = currentRoom->ConnectionSouth;
else //say cant go
if(input == wants to go east)
if(can go east)
currentRoom = currentRoom->ConnectionEast;
else //say cant go
if(input == wants to go west)
if(can go west)
currentRoom = currentRoom->ConnectionWest;
else //say cant go
}
I also added the null values into my constructor (I didn't show it, I didn't think it was nessecary)
and have values to check if there is an available room
if I start from A I can go to B and back to A as much as I want,
then if I can go from B to C, but If I try to go back to A after being in C
when I go south it puts me in a strange Location that I didn't create and all available locations crash it.
so, A->B->A->B->C->B->(Strange area that isn't A)->Crash