Going beyond bounds problem
Hey guys
Happy new year first off!
I am creating the tortoise/hare sim but seem to be running into a bit of a problem. The code compiles fine, and the program runs for the 30 seconds no problem.
The issue comes at the end I get a "stack around 'racetrack' was corrupted' seg fault which generally means I have gone out of the bounds of the one dimensioanal array.
The thing is, I did my best to prevent this happening by passing a single addtional varialbe to each movement function to test if the new value was
below zero or over the array's allocated size - then it would return nothing and quit the function without changing the position of the character.
Here was what I mean:
Code:
// function to move the turtle three squares to the right
std::string turtFastPlod ( char rctrk[], const char *tur, int &rtl ) {
std::string str = "\n\nTURTLE: FAST PLOD\n";
rtl += 3; // add the value to reference
// check if beyond array bounds - if so quit action
if (( rtl < 0 ) || ( rtl > 69 )) {
rtl = 0;
return "";
}
rctrk[ +3 ] = *tur;
return str;
}
And this is part of the main game functon that calls this:
Code:
int playGame ( char rctrk[], const char *tur, const char *hre ) {
// these are used to check if the T or H go beyond array
// boundarys
int turtleLimit = 0,
hareLimit = 0;
for ( int i = 0; i <= 30; ) {
for ( int j = 1; j <= 3; j++ ) {
std::cout << "\n";
}
int eventChoice = ( 1 + rand() % 8 );
std::cout << std::setw ( 52 ) << announceStart() << "\n\n";
std::cout << "\nSeconds Lapsed: " << i << "\n\n";
printRaceCourse ( rctrk );
switch ( eventChoice ) {
case 1:
std::cout << turtFastPlod ( rctrk, tur, turtleLimit );
break;
The race track array passed from main is
Code:
const int ARRAY_SIZE = 70;
char raceTrack[ ARRAY_SIZE ] = { 0 };
I must be somthing simple I am doing wrong - I have trying to suss it for a
while and cannot understand what I am doing wrong. Any advice greatly
appriciated.