This is an assignment from school, I have spent a week bashing my head against the wall atrying to figure out how to get this to work. The program reads in a 'maze' or X's and stuff from, a file, and my functions are to fill a pre diminshied char** array and then display it, there is a recursive maze solving problem as well but I think I have that figured out but I wont know till I can get it to display in the first place! let me know if anyone has any pointers thankS!

void Maze::fill(const char fileName[])
{
    ifstream inFile;
    int row = 0, newRows = 0, newCols = 0;
    string buffer;

    // Attempt to open the file
    inFile.open(fileName);
    if (inFile.fail())
    {
        throw exception("Failed to open input file");
    }

    // Get the number of maze rows and columns
    // and allocate the maze grid
    inFile >> newRows >> newCols;
    setRows(newRows);
    setCols(newCols);
    grid = new char*[getRows()];
    for (row = 0; row < getRows(); ++row)
    {
        grid[row] = new char[getCols()];
    }

    // Move past the trailing new line after the column 
    getline(inFile, buffer);

    // Fill in the grid for the maze
    row = 0; // start at the first row of the grid
    while (!inFile.eof())
    {
        // Get a row of grid data
        getline(inFile, buffer);

        // CODE NEEDED: A single inner loop
        // Loop through the length of the string buffer (buffer.length())
        // and assign the characters from buffer to each cell in
        // the current grid row. If encountering a START or EXIT character, 
        // then also call setStart or setExit accordingly.


        for(int x = 0 ; x < buffer.length() ; x++)
        {
            (grid[row][x]) = buffer[x] ;

            if ((grid[row][x]) == START)
                setStart(row,x);

            if((grid[row][x]) == EXIT)
                setExit(row,x);


        }

    //  display();

        row++; // next row in the grid
    }

    inFile.close();

}

HERE IS THE DISPLAY FUNCTION




void Maze::display()
{
    for (int x = 0 ; x < getRows() ; x ++)
    {
        for (int y = 0 ; y < getCols() ; y++)
        {
            cout<<grid[x,y];
        }
        cout<<endl;
    }
}

any help would be greatly appreciated!!! thanks!

Recommended Answers

All 2 Replies

let me know if anyone has any pointers thankS!

0x0F021016
0x0E031418
0x08051033

Ba dum tss

any help would be greatly appreciated!!! thanks!

Anyway, at a quick first glance I don't really see anything that could cause errors. What have you established yourself already? At what point do problems arise?

-edit-

Actually, replace

cout<<grid[x,y];

with

cout<<grid[x][y];

I don't quite get where's your problem, but I see you're trying to insert from a file, a matrix, and than display it:
Probably the text file looks like this:

5 7
*******
*S*****
*****E*
*******
*******

where 5 is the number of rows, 7 the number of columns, and the rest is the actual matrix. Correct me if I'm wrong.
So you need a loop to asign the items from the matrix:

    void read(){
        f_in >> DimX >> DimY;
        for(int i=0;i<DimX;++i)
            f_in >> Mat[i];
    }

This is a simple function that reads the items from a file, where f_in is the ifstream descriptor of that file, and Mat is a 2dimensional char matrix. DimX will be the X's and DimY will be the Y's.
Also, if you want to asign start/end points:

void init(){
        for(int i=0;i<DimX;++i)
            for(int j=0;j<DimY;++j)
                if (Mat[i][j] == 'S') setStart(i, j);
                if (Mat[i][j] == 'E') setExit(i, j);
    }

As a quick display function, you can do like this:

void print(){
    for (int i=0;i<DimX;i++)
        cout<<Mat[i]<<endl;
}

Also, line 75 from your code:

cout<<grid[x,y];

it's

cout<<grid[x][y];

You go to the row x, and column y, it's not a pair.

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.