I am trying to make a maze program. It is suppose to display the maze before it is solved, then solved. After that is should be able to random generate a different maze that needs to be solved. My problem is that when I compile I get this, Any suggestions would be great. Thank you. :

try.cpp: In function âvoid findpath(int (*)[50], int, int, int, int)â:
try.cpp:92: error: return-statement with a value, in function returning 'void'
try.cpp:97: error: return-statement with a value, in function returning 'void'
try.cpp:102: error: return-statement with a value, in function returning 'void'

#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;

#define N 50    //Defines the column to be able to use
#define M 50    //Defines the rows to be able to use
#define PATH 0  //Path will start at 0
#define BLOCK 1
#define TRUE 1
#define FALSE 0

void createmaze(int maze[N][M]);      // this creates the maze with walls and holes for the start and finish
void printmaze(int maze[N][M],int ht,int wt); // this prints out the initial maze
void findpath(int maze[N][M],int high,int wid, int x, int y); // this finds the path from start to finish
int wall(int maze[N][M], int x, int y); // this finds if there is a wall north,south,east,west

int main()
{
        srand((unsigned)time(NULL));

        int maze[N][M];
                int width, height;
                cout<<"How big do you want the height"<<endl;
                cin>>height;
                cout<<"How big do you want the width"<<endl;
                cin>>width;
                cout<<"This is the maze first look:"<<endl;
                createmaze(maze);
                printmaze(maze,height,width);
                cout<<"This is maze with the path found"<<endl;
                findpath(maze,height,width,1,1);
                printmaze(maze,height,width);


return 0;
}
int wall(int maze[N][M],int x, int y)
{
         if(maze[x - 1][y]  == BLOCK
       && maze[x][y - 1] == BLOCK
       && maze[x][y + 1] == BLOCK
       && maze[x + 1][y] == BLOCK)
        {
                return 1;
        }else {
        return 0;
        }
}

void createmaze(int maze[N][M])
{            for(int a = 0; a < N; a++)
             {
                 for(int b = 0; b < M; b++)
                 {
                     if(a % 2 == 0 || b % 2 == 0)
                         maze[a][b] = 1;
                     else
                         maze[a][b] = PATH;
                 }
             }
}

void printmaze(int maze[N][M], int ht, int wt)
{
                cout<<" MAZE: ";
        for(int c =0; c< ht; c++)
        {       for(int d; d < wt; d++)
                {       if( maze[c][d] == BLOCK)
                        {       cout<< "+";
                        }else
                        {       cout<< " ";
                        }
                }
                        cout<<"\n";
        }
}

void findpath(int maze[N][M], int high, int wid,int x, int y)
{
    // If x,y is outside maze, return false.
    if ( x < 0 || x > high- 1 || y < 0 || y > wid - 1 )
    {           return FALSE;
    }
        // If x,y is the goal, return true.
        if ( maze[x][y] == 'G')
        {
                 return TRUE;
        }

        // If x,y is not open, return false.
    if ( maze[x][y] != '.' && maze[x][y] != 'S' )
    {           return FALSE;
        maze[x][y] = '+';       // Mark x,y part of solution path.

    }

        // If find_path North of x,y is true, return true.
    if ( findpath(x, y - 1) == 1 )
    {   return TRUE;
    }

        // If find_path East of x,y is true, return true.
    if ( findpath(x + 1, y) == 1 )
    {   return TRUE;
    }

        // If find_path South of x,y is true, return true.
    if ( findpath(x, y + 1) == 1 )
    {           return TRUE;
    }

        // If find_path West of x,y is true, return true.
    if ( findpath(x - 1, y) == 1)
    {   return TRUE;
        maze[x][y] = 'x';    //mark the repeat steps
    }


    return FALSE;

}

ok i did that and now i get this error

try.cpp: In function âint findpath(int (*)[50], int, int, int, int)â:
try.cpp:108: error: invalid conversion from âintâ to âint (*)[50]â
try.cpp:88: error: too few arguments to function âint findpath(int (*)[50], int, int, int, int)â
try.cpp:108: error: at this point in file

In your findpath function where you do this findpath(x, y - 1) == 1 ) , you need to pass it the maze and other variables as well. Change it to findpath(maze,high,wid,x, y - 1) == 1 )

Edited 5 Years Ago by firstPerson: n/a

ok thank you! that took away the errors i had!. now for my result when i input the height and width i get a blank maze. should i make a skeleton or something to how it should look?

well i want the out put to look like this
"############",
"#+++#++++++#",
"S+#+#+####+#",
"###+#++++#+#",
"#++++###+#+G",
"####+#+#+#+#",
"#++#+#+#+#+#",
"##+#+#+#+#+#",
"#++++++++#+#",
"######+###+#",
"#++++++#+++#",
"############",
but then when you run it again it is a different maze that is generated. i am not to sure what is making it to where it isnt working.

alright i started over with my maze program. as of right now i just want my create maze and print maze to work before i try to do the findpath part of it. this is what i have so far but what i want it to do is print out this type of maze say its a 3x3

#+#
##+
++#
which it doesnt give me. how can i fix it to do that?

#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <time.h>
using namespace std;

#define N 5
#define M 5

void Createmaze(char maze[N][M],int row, int col);
void Printmaze(char maze[N][M],int r, int c);
int main()
{
        char maze[N][M];
        int row, col;

        cout<<"how many rows u want: "<<endl;
        cin>>row;
        cout<<"how many cols u want: "<<endl;
        cin>>col;
        Createmaze(maze, row, col);
        Printmaze(maze, row, col);

return 0;
}

void Createmaze(char maze[N][M], int row, int col)
{
        for ( int i =0; i <= row; i++)
        {
                for( int j =0; j <= col; j++)
                {

                        maze[i][j] = ' ';
                }
        }

}

void Printmaze(char maze[N][M], int r, int c)
{
        int a, b;

        for( a =0; a <= r; a++)
        {
                for( b =0; b <= c; b++)
                {
                                maze[a][b]='#'; //else pass hash charater to current position in array
                                cout<<maze[a][b]; //display maze to screen
                }

        }

}

This is what you want( not compiled )

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

const int MAZE_ROW = 5;
const int MAZE_COL = 5;
void initializeMaze(char maze[MAZE_ROW][MAZE_COL], char initValue){
 for(int i = 0; i < MAZE_ROW; ++i){
  for(int j = 0; j < MAZE_COL; ++j){
     maze[i][j] = initValue;
  }
 }
}
void printMaze(const char maze[MAZE_ROW][MAZE_COL]){
  for(int i = 0; i < MAZE_ROW; ++i){
    for(int j = 0; j < MAZE_COL; ++j){
        cout <<  maze[i][j];
     }
    cout << endl;
  }
}

int main(){
 char maze[MAZE_ROW][MAZE_COL];
 const char WALL = '#';
 const char PATH = '+';
 initializeMaze(maze,WALL);
 printMaze(maze);
}

Now your next step is to implement these functions :

//generate some sort of path in the maze
//does not guarantee that path is solvable
void generatePath(char maze[MAZE_ROW][MAZE_COL]);

//solves the maze
//if solvable, it prints out the path and returns true
//else it returns false
bool solveMaze(const char maze[MAZE_ROW][MAZE_COL]);

Edited 5 Years Ago by firstPerson: n/a

Thank you for showing me that. It does help which I am trying to implement into my program. My problem is getting the maze to display the maze as such
+ +-+
| + +
+-+-+
I think it will have to be in the generatemaze function but I am not too sure though.

This article has been dead for over six months. Start a new discussion instead.