exception in my program

exception: std::out_of_range at memory location

not sure what this means. below is the function that it yells at me. Exception happens after the System("pause");(i use pause to debug)
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
void run(int a,int b, bool& state){// (x,y,pervious direction)
	// display row col grid
	// check if surrounding spaces are the exit,if so return number of spaces traveled
	// check if any openings up,right,bottom,down
	// if direction is open then run in that direction with new position
	// if all directions closed then trapped at memory location 
	maze[a].at(b) = '@';

	cout << "      Row: " << a << "    Col: " << b << endl;
	cout << " 0123456789" << endl;
	for(int i = 0; i < 10; i++){
		cout << i << maze[i] << endl;
	}
	if(a != 0){
		if(maze[a--].at(b) == 'E'){state = true;return;}}
	if(b != 9){
		if(maze[a].at(b++) == 'E'){state = true;return;}}
	if(a != 9){
		if(maze[a++].at(b) == 'E'){state = true;return;}}
	if(b != 0){
		if(maze[a].at(b--) == 'E'){state = true;return;}}
	system("pause");
	if(a != 0){
		if(maze[a--].at(b) != '+' && maze[a--].at(b) != '@' && a != 0){run(a--,b,state);}}
	if(b != 9){
		if(maze[a].at(b++) != '+' && maze[a].at(b++) != '@' && b != 9 && state == false){run(a,b++,state);}}
	if(a != 9){
		if(maze[a++].at(b) != '+' && maze[a++].at(b) != '@' && a != 9 && state == false){run(a++,b,state);}}
	if(b != 0){
		if(maze[a].at(b--) != '+' && maze[a].at(b--) != '@' && b != 0 && state == false){run(a,b--,state);}}
	
	return;

}

thanks for any help everyone
Last edited on
Try to use a debugger instead.
http://www.cplusplus.com/reference/stl/vector/at/
So b<0 or b>=maze[_].size()

Edit: Sorry, I could never understand this behaviour:
maze[a--].at(b) != '+' && maze[a--].at(b) != '@' && a != 0 What is the value of a in every evaluation?
Last edited on
maze[] is an array of strings with a and b being a position within that 10x10 array. + represents a wall and '@' is where you have already gone. this is a maze program. get to the E and your safe but the program does it itself recursively

so as long as the next position (up,left,down,right) isn't a wall or you have not gone that direction or it is not out of bounds of the array then keep going in that direction
Last edited on
The exception is telling that b is out of bounds.
(I'm not sure of this) Suppose that a=6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
if(a != 9){
		if(maze[a++].at(b) != '+' && //at the end of the line a=7
                maze[a++].at(b) != '@' && //at the end of the line a=8
                a != 9 && state == false){
                    run(a++,b,state); //at the end of the line a=9
                }
        }
	if(b != 0){
		if(maze[a].at(b--) != '+' && 
                maze[a].at(b--) != '@' && 
                b != 0 && state == false){
                     run(a,b--,state); //here you are analysing in a=9, you jump from 6 to 9 (that's cheat)
                }
       }


Just check the neighbours with a+1, instead of a++ (I think that the secondary effect is causing you trouble)
yeah i got that now, thanks i got it working now
Topic archived. No new replies allowed.