Why is vector subscript out of range?

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <cstdlib>
#include <vector>
#include <ctime>
struct thing{
	int X, Y;
};
thing player, goal;
std::vector<thing> trap;
std::vector<thing> enemy;
char grid[10][10];
int level;
void resetGame() {
	bool failure;
	do {
		std::cout << "Choose level: ";
		int temp = NULL;
		std::cin >> temp;
		if (std::cin.fail()) {
			std::cout << "Invalid input.\n";
			std::cin.clear();
			std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
			failure = true;
		}
		else {
			failure = false;
			level = temp;
		}
	} while (failure);
	player.X = 0, player.Y = 0, goal.X = 9, goal.Y = 9;
	thing foo;
	trap.erase(trap.begin(),trap.end());
	for (int i = 0; i < level; i++) {
		foo.X = rand() % 10;
		foo.Y = rand() % 10;
		if ((foo.X == 0 && foo.Y == 0) || (foo.X == 9 && foo.Y == 9)) {
			i--;
		}
		else {
			trap.push_back(foo);
		}
	}
	enemy.erase(enemy.begin(), enemy.end());
	for (int i = 0; i < level; i++) {
		foo.X = rand() % 10;
		foo.Y = rand() % 10;
		if ((foo.X == 0 && foo.Y == 0) || (foo.X == 9 && foo.Y == 9)) {
			i--;
		}
		else {
			enemy.push_back(foo);
		}
	}
}
void displayInfo() {
	std::cout << "\nUse arrow keys to move 'P' to the goal, 'G'. Avoid any traps and enemies along the way. Press 'R' while playing to restart. The level corresponds with the frequency of traps.\n\nPress -> to continue.";
	for (;;) {
		if (_getch() == 77) {
			return;
		}
	}
}
void moveEnemy() {
	for (int i = 0; i < level; i++) {
		switch (rand() % 4) {
		case 0:
			if (enemy[i].Y > 0)
				enemy[i].Y--;
			break;
		case 1:
			if (enemy[i].Y < 9)
				enemy[i].Y++;
			break;
		case 2:
			if (enemy[i].X < 9)
				enemy[i].X++;
			break;
		case 3:
			if (enemy[i].X > 0)
				enemy[i].X--;
			break;
		}
	}
}
int main()
{
	srand((int)time(NULL));
	resetGame();
	for (;;) {
		beginning:
		system("cls");
		for (int i = 0; i < 10; i++) {
			for (int j = 0; j < 10; j++) {
				grid[i][j] = '.';
			}
		}
		for (int i = 0; i < level; i++) {
			for (int j = 0; j < level; j++) {
				if (enemy[i].X == trap[j].X && enemy[i].Y == enemy[i].Y) {
					enemy.erase(enemy.begin() + i);
					trap.erase(trap.begin() + i);
				}
			}
		}
		grid[goal.X][goal.Y] = 'G';
		grid[player.X][player.Y] = 'P';
		for (int i = 0; i < level; i++) {
			grid[trap[i].X][trap[i].Y] = 'X';
		}
		for (int i = 0; i < level; i++) {
			grid[enemy[i].X][enemy[i].Y] = 'E';
		}
		for (int i = 0; i < 10; i++) {
			for (int j = 0; j < 10; j++) {
				std::cout << grid[j][i] << "   ";
			}
			std::cout << "\n\n";
		}
		std::cout << "Press 'i' for info.\n";
		for (int i = 0; i < level; i++) {
			if (player.X == trap[i].X && player.Y == trap[i].Y) {
				std::cout << "You lose. Restart? (Y or N)\n";
				for (;;) {
					switch (_getch()) {
					case 121:
						resetGame();
						goto beginning;
					case 110:
						return 0;
					}
				}
			}
		}
		for (int i = 0; i < level; i++) {
			if (player.X == enemy[i].X && player.Y == enemy[i].Y) {
				std::cout << "You lose. Restart? (Y or N)\n";
				for (;;) {
					switch (_getch()) {
					case 121:
						resetGame();
						goto beginning;
					case 110:
						return 0;
					}
				}
			}
		}
		if (player.X == goal.X && player.Y == goal.Y) {
			std::cout << "You Win! Play again? (Y or N)\n";
			for (;;) {
				switch (_getch()) {
				case 121:
					resetGame();
					goto beginning;
				case 110:
					return 0;
				}
			}
		}
		else {
			switch (_getch()) {
			case 72:
				moveEnemy();
				if (player.Y > 0)
					player.Y--;
				break;
			case 80:
				moveEnemy();
				if (player.Y < 9)
					player.Y++;
				break;
			case 75:
				moveEnemy();
				if (player.X > 0)
					player.X--;
				break;
			case 77:
				moveEnemy();
				if (player.X < 9)
					player.X++;
				break;
			case 32:
				moveEnemy();
				break;
			case 105:
				displayInfo();
				break;
			case 114:
				std::cout << "Are you sure you want to restart? (Y or N)\n";
				for (;;) {
					switch (_getch()) {
					case 121:
						resetGame();
						goto beginning;
					case 110:
						goto beginning;
					}
				}
			}
		}
	}
}

This part of this code for some reason doesn't work and causes the subscript out of range error at seemingly random points (more likely if you input a higher level):
1
2
3
4
5
6
7
8
for (int i = 0; i < level; i++) {
			for (int j = 0; j < level; j++) {
				if (enemy[i].X == trap[j].X && enemy[i].Y == enemy[i].Y) {
					enemy.erase(enemy.begin() + i);
					trap.erase(trap.begin() + i);
				}
			}
		}

I need this code in order to make it so that when an enemy occupies the same space as a trap, the enemy sets off the trap and both disappear. Other than this, the code works perfectly.

In case you need to know, this was compiled as Win32 using VisualStudios.
Last edited on
I Would suggest you use a debugger and go through your program step by step, I assume you know where it crashes. Your error means that you are accessing memory you dont have. So step through it one line at a time and figure out why that happens.
I fixed it. The problem was that I assumed level to always be the length of the vector.
I fixed it. The problem was that I assumed level to always be the length of the vector.

Any time you're using erase on a container in a loop, you need to double check and make sure things are being done correctly.
Hi,

There are a bunch of things I would fix:

You need to use functions more, if you have 6 levels of nesting then you know you can improve things. Candidates for functions are compound statements - statements in braces. So this could be the bodies of a for loop, the body of an if statement etc. Also, anything that you have repeated, like restart. And Menus, can be in a function.

You should also have a default: case for each switch, to catch bad input.

Also, goto is tempting, but bad news. One can return early from void functions.

Consider a range based for loop if you want to process all the items in a container. Google to see how that works :+) The advantage is that you won't go out of bounds.

I would also avoid infinite conditions on loops where ever possible, the one on line 192 doesn't do anything: both cases cause the program to jump elsewhere. If neither case is satisfied you have what you don't want: an infinite loop. It shouldn't be hard to come up with an end condition.

You can have a char in a switch, those numbers are bit cryptic.

Hope this helps :+)
Topic archived. No new replies allowed.