Search code examples
c++maze

C++: Read text file into 2d array


I'm trying to read the following maze.txt file:

35
35
0
10
++++++++++S++++++++++++++++++++++++
++++++++++OOOOOOOOOOOOOOOOOOOOOOOOO
++++++++++O++++++++++++++++++O+++++
OOOOOOOOOOOOOOOOOOOOOOOOOO+++O++OOE
O+++++++++O++++++++++++++O+++O++O++
OOOOOO++++O++++++++++++++O+++O++O++
O++++O++++OOOOOOOOOOO++++O+OOO++O++
O++++O++++O+++++++++OOOO+O+++O++O++
OOO++OOOO+OOOOOO+++++++++++OOO++OOO
O+O+++++O++++++OOOOOOOOOO++O++++++O
O+OOOO++O++++++O++++++++O+++OOO+++O
O++++O++OOOOOOOO++++++++O+++O+O+++O
OOO++O++++++++++++++++++OOOOO+O+++O
++O++OOOOOOOOOOOOOOOO+++++++++OO++O
OOO+++++++++++++++++OOOOOO++++++++O
O++++++++++++++++++++++++O++OOOOOOO
+++++++++++++++++++++++++O++O++++++
OOOOOOOOOOOOOOOOOOOOOOOOOO++OOOOO++
O++++++++++++++++++++++++O++++++O++
OOOOOOO+++++++++++++++OOOOOOO+++O++
++++++++++++++++++++++O+++++OO++O++
OOOOOOOOOOOOOOOOOOOOOOO++++++O++O++
O++++++++++++++++++++++++++++O+OOOO
OOOO++++++++++++++++++++OOOOOO+O+++
+++OOOOOOOOO+++++++++++++++++++O+++
+++++O+++++OOOOOOOOOO++++++++OOO+++
+O+++OOOOO++++++O++++++++++++O+++++
+OOOOO+++O++++++OOOOOO+++++++O+++++
+++++++++++++++++++++OOOOOOOOO+++++
OOOOOOOOOOOOOOOOOOOOOO+++++++++++++
+++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++

The code works fine with the maze inside the code but I moved it out to a text file, which seems to be read but it is not working. It's giving me the error:

No matching function for call to 'mazeTravel'.

I'm not sure where to go from here. Any help would be appreciated!

#include <iostream>
#include <fstream>

using namespace std;

void printMaze(const char maze[][12], int xCoordinate, int yCoordinate);
int mazeTravel(char maze[][12], int xCoordinate, int yCoordinate, int direction);

int main()
{
    char maze[35][35];
    ifstream file;
    file.open("maze.txt");
    if (!file) {
        cout << "Error reading file\n";
        return -1;
    }
    else {
        for (int row = 0; row < 35; row++) {
            for (int column = 0; column < 35; column++) {
                file >> maze[row][column];

                int success = 0;
                success = mazeTravel(maze, 2, 0, 1);
                if (success == 1)
                    cout << "The maze has been solved.\n";
                else
                    cout << "Sorry, the maze cannot be solved\n";
            }
        }
    }
    return 0;
}

Solution

  • You could use a std::vector of std::strings to represent your maze:

    std::vector<std::string> maze;
    

    To access its cells use

    maze[row][column];  // with row being y and column x
    

    To get the number of rows use

    maze.size()
    

    and

    maze[0].size()
    

    for the number of columns.

    You could read such a maze like that (without error checking to not clutter the code):

    std::vector<std::string> readMaze(std::istream &is)
    {
        std::size_t columns;        
        std::size_t rows;
        is >> columns >> rows;
    
        int foo;             // sorry, don't know what the 3rd and 4th 
        is >> foo >> foo;    // number is. a starting position, perhaps?
    
        // ignore the rest of the current line:
        is.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    
        std::string line;
        std::vector<std::string> maze;
    
        while (std::getline(is, line))
            maze.push_back(line);
    
        return maze;
    }
    

    An implementation (with error checking) could look like that:

    #include <cstdlib>  // EXIT_FAILURE
    #include <limits>   // std::numeric_limits<>
    #include <vector>
    #include <string>
    #include <fstream>
    #include <iostream>
    
    // to not have to type std::vector<std::string> all over the place
    using maze_type = std::vector<std::string>;
    
    void printMazeCell(maze_type const &maze, std::size_t x, std::size_t y)
    {
        std::cout.put(maze[y][x]);
    }
    
    void printMaze(maze_type const &maze)
    {
        for (auto const &row : maze)
            std::cout << row << '\n';
    }
    
    int mazeTravel(maze_type const &maze, std::size_t x, std::size_t y, int dir)
    {
        // access cells of the maze with maze[y][x]
        // maze.size() for the number of columns and
        // maze[0].size() for the number of rows
        return 42;
    }
    
    maze_type readMaze(std::istream &is)
    {
        std::size_t columns;
        if (!(is >> columns)) {
            std::cerr << "Couldn't read the number of columns :(\n\n";
            return maze_type{};  // return an empty maze on error
        }
    
        std::size_t rows;
        if (!(is >> rows)) {
            std::cerr << "Couldn't read the number of rows  :(\n\n";
            return maze_type{};
        }
    
        int foo;
        is >> foo >> foo;  // sorry, don't know what the 3rd and 4th number is
    
        // ignore the rest of the current line:
        is.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    
        std::cout << "Trying to read a maze with " << columns << " columns ...\n";
    
        std::string line;
        maze_type maze;
        while (std::getline(is, line)) {
            if (line.length() != columns) {
                std::cerr << "Found a row that contains only "
                    << line.length() << " columns :(\n\n";
                return maze_type{};
            }
            maze.push_back(line);
        }
    
        if (maze.size() != rows) {
            std::cerr << "The maze only consists of "
                      << maze.size() << " rows :(\n\n";
            return maze_type{};
        }
    
        return maze;
    }
    
    int main()
    {
        char const *filename = "maze.txt";
        std::ifstream is{ filename };
        if (!is.is_open()) {
            std::cerr << "Couldn't open \"" << filename << "\" for reading :(\n\n";
            return EXIT_FAILURE;
        }
    
        maze_type maze = readMaze(is);
    
        if (!maze.size()) {  // readMaze returned an empty maze :(
            std::cerr << "Bye.\n";
            return EXIT_FAILURE;
        }
    
        printMaze(maze);
    }