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.
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.
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.