I am new to C++ and am trying to write a program that reads a file, dynamically creates a 2D array, and fills the array with input from the file. Then it solves the maze using recursion. The text file looks like this:

[![The "maze.txt" file](https://i.stack.imgur.com/W2IXE.png%27)](https://i.stack.imgur.com/W2IXE.png%27)

The first 4 numbers dictate the amount of rows, amount of columns, and starting position (X,Y). "S" is the start, "O" the path, and "E" the end. I have no trouble with creating the array or filling the array, but when running the program, it encounters an error when calling the recursive function to solve the maze. This is the function:

```
bool mazeSolve(char **maze, int startRow, int startCol){

  char test = maze[startRow][startCol];

  //If maze finds end stop program (Base Case)
  if (test == 'E'){
     cout << "End reached" << endl;
     return true;
  }//End if for base case

  //If function can go right
  if (maze[startRow][startCol+1] == 'O'){
     maze[startRow][startCol] = 'P';
     return mazeSolve(maze, startRow, startCol+1);
  }

  //If function can go left
  if (maze[startRow][startCol-1] == 'O'){
     maze[startRow][startCol] = 'P';
     return mazeSolve(maze, startRow, startCol-1);
  }

  //If function can go up
  if (maze[startRow-1][startCol] == 'O' && startRow-1 >= 0){
     maze[startRow][startCol] = 'P';
     return mazeSolve(maze, startRow-1, startCol);
  }

  //If function can go down
  if (maze[startRow+1][startCol] == 'O'){
     maze[startRow][startCol] = 'P';
     return mazeSolve(maze, startRow+1, startCol);
  }

  //If function cannot find O's then it will backtrack
  //If function reaches wall, backtrack down
  if (maze[startRow+1][startCol] == 'P'){
        maze[startRow][startCol] = 'C';
        return mazeSolve(maze, startRow+1, startCol);
  }

  //If function reaches wall, backtrack up
  if (startRow-1 >= 0 && maze[startRow-1][startCol] == 'P'){
        maze[startRow][startCol] = 'C';
        return mazeSolve(maze, startRow-1, startCol);
  }

  //If function reaches wall, backtrack left
  if (maze[startRow][startCol-1] == 'P'){
        maze[startRow][startCol] = 'C';
        return mazeSolve(maze, startRow, startCol-1);
  }

  //If function reaches wall, backtrack right
  if (maze[startRow][startCol+1] == 'P'){
        maze[startRow][startCol] = 'C';
        return mazeSolve(maze, startRow, startCol+1);
  }      

```

I have googled and searched this site for similar problems but I seem to have a fundamental misunderstanding of recursion as I can't follow the solutions. I thought the bottom half would allow the function to backtrack until it found a new "O" and eventually an "E", but it is looping infinitely. My professor is asking for this to be done with if statements. Where did my logic go wrong? Any help would be greatly appreciated.