Sign in to follow this  

Access Violation - text based game

This topic is 3459 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
room[x].setRoomDescription(description = line);

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]

Share this post


Link to post
Share on other sites
Couple o' things:

// 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.

Share this post


Link to post
Share on other sites
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 objects
Rooms 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.

Share this post


Link to post
Share on other sites

This topic is 3459 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this