Access Violation - text based game
Im getting an access violation on a simple text based game ive been working on to refresh my c++ coding skill, as i quit in my second year of uni (dont ask) and then decided to retake it now and carry on.
im trying to get the game to the point where you can navigate through the rooms, encounter an enemy in a certain room, make a decision (a,b,c or d) where 3 of them kill the enemy and one of them kills you (i can implement that later), and then the room has a different description if the enemy is dead, and you can exit the rooms via 2 exits through two of the rooms.
ive highlighted the part of the code that i get pointed to when i break to debug after the access violation, and ive highlighted a part of the code (the wierd boolean) that seems to fix the code if i delete the line.
http://gamedev.pastebin.com/m24c12459
im not a very experienced debugger, hopefully if i run into another access violation in the future then tackling this will help me out a great deal
cheers
room[x].setRoomDescription(description = line);
should read...
room[x].setRoomDescription(line);
you may also have some funky results from:
when calling it via : room[currentRoom].returnRoomDescription(currentRoom);
your room[currentRoom] is defining the currentRoom info, you could drop off the (int currentRoom) from the returnRoomDescription because your already selecting the current room through selecting the room's vector.
[Edited by - Kalten on June 25, 2008 6:08:29 PM]
should read...
room[x].setRoomDescription(line);
you may also have some funky results from:
#//Method which returns the number of the room#void Rooms::returnRoomNumber(int currentRoom)#{# cout << "room number = " << currentRoom << "\n\n";#}#
when calling it via : room[currentRoom].returnRoomDescription(currentRoom);
your room[currentRoom] is defining the currentRoom info, you could drop off the (int currentRoom) from the returnRoomDescription because your already selecting the current room through selecting the room's vector.
[Edited by - Kalten on June 25, 2008 6:08:29 PM]
Couple o' things:
That's as far as I've looked in the code, but maybe that'll help you to get things working.
// I don't think this is the cause of the problem, but the following code:/*ifstream myFile;myFile.open ("descriptions.txt", ios::in);if (!myFile.is_open()){ cout << "Could not find descriptions.txt\n\n";}*/// Can be written more idiomatically as:ifstream myFile("descriptions.txt");if (!myFile) { cout << "Could not find descriptions.txt\n\n";} Rooms room[25];//loop to set room numbers and descriptions//for (int x = 1; x != 26; x++)// It looks like you're coming from a language with 1-based indexing,// but in C and C++, indexing is zero-based. What you want here is:for (int x = 0; x < 25; ++x) { ... }// I'd also recommend using std::vector or another array class that offers// the option of bounds checking (which most likely would have caught this// mistake before it blew up on you).
That's as far as I've looked in the code, but maybe that'll help you to get things working.
Quote:Original post by Kalten
room[x].setRoomDescription(description = line);
should read...
room[x].setRoomDescription(line);
No, actually that is perfectly fine. It is equivalent to
description = line;room[x].setRoomDescription(description);
The problem is here:
//create room objectsRooms room[25];for (int x = 1; x != 26; x++){// ...}
The last valid position in the room array is 24. In the for loop you try to access array position 25, which is invalid. That line should correctly read:
for (int x=1; x < 25; x++)
You also could use x!=25, since it's an integer variable and you are likely going to hit the value 25 exactly, but getting used to using '<' in these situations is a good idea.
EDIT: beaten to it.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement