Jump to content
  • Advertisement
Sign in to follow this  
Slider38

Help - the disappearing array element

This topic is 5167 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

I am using an array to track game turns based on 49 map areas. I put together a segment to randomize the turns each round and hold them in turn_list[]. Testing has found that the last element - turn_list[49] is being dropped for some reason. I am losing it within a double do loop used for game play. At the top of the loop, the element is there. I can print it to the screen. But somewhere within the loop, it's getting erased - weird. Has anyone got an idea why this would occur? The loop looks basically like this... do { do { This code controls a menu of player choices and sets a value for a variable used in a switch to send the program to perform the desired task. When I print the last array element from this location, it's gone. One choice here is "save game". While testing the save stuff, I found the 49th turn_list element was being lost. } } While(g==0) The do within the do allows the player to change his mind as to what he wants to do, under certain conditions, without losing his turn. Part of the save game has to include the turn_list so I can tell where the game left off. I don't know why the last element is being lost (stepped on?). Any thoughts would be appreciated. Thanks, Slider

Share this post


Link to post
Share on other sites
Advertisement
C and most of its relatives use 0 based arrays. turn_list[0] is the first element and turn_list[48] would be the 49th element. So attempting to access turn_list[49] would be bad mojo.

Share this post


Link to post
Share on other sites
How do you declare your array:

1:
/* valid elements: 0 - 48 */
int turn_list[49];

*OR*

2:
/* valid elements: 0 - 49 */
int turn_list[50];

?

The latter would be the way to go if you want to have your first turn be accessed as turn_list[1], although you are technically wasting the true first element in the array: turn_list[0]. Big deal.

If your first item in the array is going to be turn[0], then declare your array as was done in #1.

Share this post


Link to post
Share on other sites
Thank you for your replys guys. A very stupid mistake, but probably all too common. As you'all suggested, my array was under-initialized by 1 element. I know better than that :>)
Thanks again guys. I'll be watching for that 0 element in the future. Guess I just got lost in all the code. Slider

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!