-4

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;
}
Swordfish
  • 12,971
  • 3
  • 21
  • 43
RochNoure
  • 77
  • 1
  • 6
  • 1
    You have to read the first some numbers at the beginning of the file. `35 35 0 10 `. After that you can read the matrix from the file. – Peter Nov 27 '18 at 13:31
  • Please don't post pictures of text. Include the file in your question. – Swordfish Nov 27 '18 at 13:31
  • @Peter That may be my next problem, but I don't think that's why I'm getting that error message, any thoughts? – RochNoure Nov 27 '18 at 13:41
  • @Swordfish I edited my question – RochNoure Nov 27 '18 at 13:41
  • @RouchNoure The problem is the `mazeTravel` function has no implementation. One other thing: Please fix the indentation of the source to make it more readable – Peter Nov 27 '18 at 14:01
  • Whats the 3rd and 4th number in the file? – Swordfish Nov 27 '18 at 14:12
  • 1
    `No matching function for call to 'mazeTravel'` in main you have `char maze[35][35];` not [something][12] that you have here `int mazeTravel(char maze[][12], int xCoordinate, int yCoordinate, int direction);` – drescherjm Nov 27 '18 at 14:19
  • @Swordfish I was wondering about the same thing myself. I saw your code below, it is very helpful, thank you!! – RochNoure Nov 27 '18 at 22:24
  • @RochNoure Now that i looked at the maze again it's quite obvious that its the starting position (0/10) is the `S` in the first row. Don't you need the starting position to find your way? did you find a way yet how you get it out of `readMaze()`? – Swordfish Nov 27 '18 at 22:38

2 Answers2

0

The problem that you don't have the implementation of

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

You should create the implementation like this:

int mazeTravel(char maze[][12], int xCoordinate, int yCoordinate, int direction)
{
    // The implementation
    return 0;
}

Another thing: You have to read the first some numbers at the beginning of the file.

35
35
0
10

After that you can read the matrix from the file

Peter
  • 1,591
  • 6
  • 19
  • I have the implementation as you pointed out, sorry for not including it as I was more focused on the file reading part. – RochNoure Nov 27 '18 at 14:04
0

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);
}
Swordfish
  • 12,971
  • 3
  • 21
  • 43