Printing a map for Snake

Hey, so I'm trying to write a snake program, and this is the code I use to generate and print the map. Eventhough the map is printed like I want it. The program errors after printing the map.

"Run-Time Check Failure #2 - Stack around the variabe 'map' was corrupted."

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
  void printMap()
{
	const int mapX = 20;
	const int mapY = 20;
	const int mapSize = mapX * mapY;

	char map[mapSize];

	for (int x = 0; mapX > x; x++)//Top and bottom
	{
		map[x] = 'X';
		map[(mapX * mapY) - x] = 'X';
	}

	for (int y = 0; mapY > y; y++)//Left and right
	{
		map[mapY * (y + 1)] = 'X';
		map[(mapY * (y + 1)) + 19] = 'X';
	}

	for (int i = 0; mapSize > i; i++)
	{
		if (map[i] != 'X')
		{
			map[i] = ' ';
		}
	}

	system("CLS");//Clears screen

	int newLine = 19;

	for (int i = 0; mapSize > i; i++)//Prints the linebreaks in the map
	{
		cout << map[i];

		if (newLine == i)
		{
			cout << endl;

			newLine = newLine + 20;
		}
	}
}
map[(mapX * mapY) - x] = 'X';
When x == 0, you are trying to access map[mapsize - 0]map[mapsize], which is out of bounds and illegal
Topic archived. No new replies allowed.