Member Avatar for Gsterminator

So here is my problem. My program's goal is to to find the shortest path for a maze in a text file. User inputs a string and then the string goes through a function that reads the file into a 2d array. My problem is that I'm seperating the sources in to a header file and a seperate source file. And because of it I can't get it to work. Please help!

My code with the functions is this: ...ps my error occurs in the readMaze funct.

    #include "maze.h"
    #include <iostream>
    #include <fstream>
    #include <string>

    using namespace std;


void Maze:: defaultArray(){
    for(int i=0;i<=ROWMAX;i++){
            for(int j=0;j<=COLMAX;j++){
                maze[i][j]='-6';
            }
        }
}
void Maze:: readMaze(string file){
    ifstream mazeFile;
    int row,col;

    mazeFile.open(file);
    if(!mazeFile.is_open()){ 
        cout << "Error! You entered an invalid filename." << endl;
        exit(1);
        //readMaze(maze,file);
    }

    mazeFile >> row >> col;
    mazeFile.get();

    for(int i=0;i<=row;i++){
        for(int j=0;j<=col;j++){
            maze[i][j]=mazeFile.get();
        }
    }
}

int Maze:: pathLength(){
    int count=0;
    for(int row=0; row<ROWMAX; row++){
        for(int col=0; col<COLMAX; col++){
            if(maze[row][col]==-4)
                count++;
        }
    }
    return count;
}

void Maze:: finish(){
    int start = 0, go = 0;
    int rows(0), cols(0);
    for(int count=0; count>-1; count++){
        if(start ==0){
            for(int row=0; row<ROWMAX; row++){
                for(int col=0; col<COLMAX; col++){       
                    if(maze[row][col]==-1){
                        for(int m=0; m>-1; m++){
                            //Define lowest number location
                            if(maze[row-1][col]==m){
                                rows=row-1;
                                cols=col;
                                maze[row-1][col]=-4;
                                start=1;
                                go=m;
                                //cout << "Up\n";
                                break;
                                //return;
                            }else if(maze[row][col+1]==m){
                                rows=row;
                                cols=col+1;
                                maze[row][col+1]=-4;
                                start=1;
                                go=m;
                                //cout << "Right\n";
                                break;
                                //return;
                            }else if(maze[row+1][col]==m){
                                rows=row+1;
                                cols=col;
                                maze[row+1][col]=-4;
                                start=1;
                                go=m;
                                //cout << "Down\n";
                                break;
                                //return;
                            }else if(maze[row][col-1]==m){
                                rows=row;
                                cols=col-1;
                                maze[row][col-1]=-4;
                                start=1;
                                go=m;
                                //cout << "Left\n";
                                break;
                                //return;
                            }else{
                                //6cout << m;
                                //go++;
                                //return;
                            }
                        }
                        if(start==1)
                            break;
                        //cout << rows++ <<" - "<< cols++ << "\n";
                    }
                }
            }
        }
        //go=0;
        //cout << "n\n";
        //Find lowest number
        if(maze[rows][cols+1]>-1)
            go=maze[rows][cols+1];
        if(maze[rows+1][cols]<go && maze[rows+1][cols]>-1)
            go=maze[rows+1][cols];
        if(maze[rows][cols-1]<go && maze[rows][cols-1]>-1)
            go=maze[rows][cols-1];
        if(maze[rows-1][cols]<go && maze[rows-1][cols]>-1)
            go=maze[rows-1][cols];
        //Define lowest number location
        if(maze[rows-1][cols]==go){
            rows=rows-1;
            cols=cols;
            if(go==0)
                return;
            maze[rows][cols]=-4;
            //cout << "Up\n" << go << "\n";
            //return;
        }else if(maze[rows][cols+1]==go){
            rows=rows;
            cols=cols+1;
            if(go==0)
                return;
            maze[rows][cols]=-4;
            //cout << "Right\n" << go << "\n";
            //return;
        }else if(maze[rows+1][cols]==go){
            rows=rows+1;
            cols=cols;
            if(go==0)
                return;
            maze[rows][cols]=-4;
            //cout << "Down\n" << go << "\n";
        }else if(maze[rows][cols-1]==go){
            rows=rows;
            cols=cols-1;
            if(go==0)
                return;
            maze[rows][cols]=-4;
            //cout << "Left\n" << go << "\n";
            //return;
        }
    }
}


void Maze:: expandArray(){
    int i;
    for(int count=0; count>-1; count++){
        for(int row=0; row<ROWMAX; row++){
            for(int col=0; col<COLMAX; col++){       
                if(maze[row][col]==count){
                    i=count+1;
                    if(runMaze(row, col+1, i)==1)
                        return;
                    if(runMaze(row+1, col, i)==1)
                        return;
                    if(runMaze(row, col-1, i)==1)
                        return;
                    if(runMaze(row-1, col, i)==1)
                        return;
                }
            }
        }
    }
}

int Maze:: runMaze(int row, int col, int input)
{
    if(maze[row][col]==-3){
        maze[row][col]=input;
    }else if(maze[row][col]==-1){
        return 1;
    }
    return 0;
}

void Maze:: convertBack(){
    for(int row = 0; row < ROWMAX; row++)
    {
        for(int col=0; col < COLMAX; col++){
            if(maze[row][col]==0){
                mazeOut[row][col]='S';
            }else if(maze[row][col]==-1){
                mazeOut[row][col]='F';
            }else if(maze[row][col]==-2){
                mazeOut[row][col]='x';
            }else if(maze[row][col]==-4){
                mazeOut[row][col]='*';
            }else if(maze[row][col]=='-6'){
                mazeOut[row][col]='-6';
            }else{
                mazeOut[row][col]=' ';
            }
        }
    }
}
void Maze:: convert(){
    for(int row = 0; row < ROWMAX; row++)
    {
        for(int col=0; col < COLMAX; col++){
            mazeOut[row][col]=maze[row][col];
        }
    }
}

void Maze:: convertArray(){
    for(int row = 0; row < ROWMAX; row++)
    {
        for(int col=0; col < COLMAX; col++){
            if(maze[row][col]=='s'||maze[row][col]=='S'){
                 maze[row][col]=0;
            }else if(maze[row][col]=='f'||maze[row][col]=='F'){
                 maze[row][col]=-1;
            }else if(maze[row][col]==' '){
                 maze[row][col]=-3;
            }else if(maze[row][col]=='-6'){
                 maze[row][col]=-6;
            }else{
                maze[row][col]=-2;
            }
        }
    }
}


void Maze::printMaze()
{
    for(int row = 0; row < ROWMAX; row++)
    {
            for(int col=0; col < COLMAX; col++){
                if(maze[row][col]!=-1 && maze[row][col]!=-2 && maze[row][col]!=-3 && maze[row][col]!=-4){
                    cout <<"  "<< maze[row][col]<<" ";
                }else{
                    cout <<" "<< maze[row][col]<<" ";
                }
            }
        cout << "\n";
    }
}

void Maze:: printMazeOut()
{
    int temp=0;
    for(int row = 0; row < ROWMAX; row++)
    {
        for(int col=0; col < COLMAX; col++){
            if(mazeOut[row][col]!='6'){
                cout << mazeOut[row][col];
                temp=0;
            }else{
                temp=1;
            }
        }
        if(temp==0)
            cout << "\n";
    }
}

Line 20: mazeFile.open(file);
It should have been mazeFile.open(file.c_str(), ios::in);

Lines: 121, 129, 137, 143 assignment to itself the rows=rows or cols=cols.
Also, post the entire error. When separating the files into headers and cpp, in the header you should place only the declaration of the function, and in the cpp the implementation:
Here's an example of header:

#ifndef MAZ_H_
#define MAZ_H_
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

class Maze{
public:
    void defaultArray();
    void readMaze(string file);
    int pathLength();
    void finish();
    void expandArray();
    int runMaze(int row, int col, int input);
    void convertBack();
    void convert();
    void convertArray();
    void printMaze();
    void printMazeOut();
};


#endif /* MAZ_H_ */

And in the cpp you just put this:

#include "maz.h"
// class implementation
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.