Reputation: 1
I have a function "printWorld" in a class that prints a 2D array called "world". Whenever I use this function and run my program in the terminal it appears to print the array but then a massive amount of random numbers and at the end says "Segmentation Fault" and closes my program. As soon as I take this function out of my code though everything works just fine.
void printWorld()
{
for(int r = 0; r <= (2*WORLD_SIZE + 1); r++)
{
for(int c = 0; r <= (2*WORLD_SIZE + 1); c++)
cout << world[r][c];
cout << endl;
}
}
This is how the function is written in the class
game.printWorld();
This is how I'm calling the function.
This is what is showing in termninal
If it helps I have this array initialized all to 0.
Upvotes: 0
Views: 159
Reputation: 21
As others have already stated, the mistake is the inner for loop where you compare 'r <= (2*WORLD_SIZE + 1)'
This causes an infinite loop.
Change the inner for loop to be:
for(int c = 0; c <= (2*WORLD_SIZE + 1); c++)
cout << world[r][c];
cout << endl;
That should do it for you : )
Upvotes: 2