Hello everyone,

I am quite new to C++ and I am currently trying to complete a project in creating a program that solves a maze of a given dimension.The text has to be read in and the first element of the file defines the size of the array,its an integer .I have defined this first element as 'size'.

example:
10
x x x x x x x x x x
x x x x p x x p g x
x p p p p x x p x x
x x x p p x x p p x
x p p p x x x x p x
x x x p p p p p p x
x p p p x x x x x x
x x x p x x x p p x
x x x p p p p p x x
x x x x x x x x x x


My idea is to create a dynamic array using vectors ,which I have done and it compiles alright.However I am a bit stumped when I try to pass the array to the maze solving function. I get this error:

'solveMaze' : cannot convert parameter 3 from 'std::vector<_Ty>' to 'char **'

I hope you guys could offer me some pointers on how to solve this problem.Thank you.

#include<iostream>
#include<string>
#include<fstream>
#include<vector>
using namespace std;

#define False 0
#define True 1

int solveMaze(int x,int y,char **maze,int size);//function declaration

int main()
{
  
 
  int m,n,i,x,y,size;
  vector< vector<char> >mazeArray;
  string filename="C:\\maze.txt";// file path
 
  
  ifstream mazeFile(filename.c_str());
 
  if (mazeFile.is_open())
  {	
	mazeFile>>size;
	cout<<size<<endl;
	mazeArray.resize(size); //resize the vector based on the size read in .
    
	for (i = 0; i <size; ++i)
		mazeArray[i].resize(size);

	while (!mazeFile.eof())
	{
		for (m=0;m<size;m++){
			for(n=0;n<size;n++)
				mazeFile>>mazeArray[m][n];}
	}
	
  }
 
  else {cout<<"There has been an error in opening the file"<<endl;}
  mazeFile.close();

  //Prints the maze in the console
  for (m=0;m<size;m++)
		{for(n=0;n<size;n++)
			cout<<mazeArray[m][n]<<" ";
			cout<<endl;		}

  cout<<"Please enter the starting coordinate(x):"<<endl;
  cin>>x;
  cout<<"Please enter the starting coordinate(y):"<<endl;
  cin>>y;
  
  solveMaze(y,x,mazeArray,size);


}

int solveMaze(int y,int x,char **maze,int size)
{
	
if (x<0||x > size-1|| y<0|| y> size-1)//choosen cord is out of range.
	return False;

if (maze[y][x]=='x') //path not open.
	return False;


if(maze[y][x]=='g') //goal has been found!
	return True;


maze[y][x]='f'; //if all conditions are met then mark the starting coordinate as valid

//Go north
if (solveMaze(x,y-1,maze,size)==True)
	return True;

//Go east
if (solveMaze(x+1,y,maze,size)==True)
	return True;

//Go south 
if (solveMaze(x,y+1,maze,size)==True)
	return True;

//Go back to west
if(solveMaze(x-1,y,maze,size)==True)
	return True;

maze[y][x]='c'; //cancels the path as part of solution

return False;
}

Just pass the vector by reference, so you will have

int solveMaze(int x,int y,vector< vector<char> > & maze,int size);

Thanks mitrmkar,

Now it compiles fine.But when I try enter my coordinates the program just crashes and the error that I get is "stack overflow".I have looked it up on the net and it says that there are 2 reasons from stack overflow :-
1.infinite recursion
2.very large stack variable

I dont think the 1 is the problem with my program because I have tested that bit,the 2nd one might be because I am passing a huge array every time I call the function.I really don't know how to overcome it though..any help guys? please.Thank you!

Post how you changed the code... I changed the prototype and the definition and it lets me enter the coordinates (then it does nothing so I'm not sure what to expect), but I don't get a stack overflow.

Just a curiosity question: how come you didn't have solveMaze return a bool? (I get your #define but it's kind of archaic when there's a bool type)

Yes I did change the char** to what mitrmkar had suggested and it compiles fine.

About the stack overflow,did you actually create a file called maze.txt and read in to your console?

Yes, I made one from your first post. It displayed on the screen when the program started. The only change that I made was that I put it in the same directory as the exe, so I took the C:\\ off of the filename string.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.