-1

Within my code, the user will input 2 values that are used to store characters from a .txt file into the vector at a certain point. To do this I used

maze[row][column] = data;

However, this causes "Debug Assertion Failed!" "Expression:Vector subscript out of range"

Please find my full code below, I'm new to using vectors so I'm not quite sure why this doesn't work.

#include<fstream>
#include<iostream>
#include<string>
#include <vector>
//Maze Size
#define SIZE 5
using namespace std;

bool isSafe( vector<vector<char>> maze, vector<vector<int>> visited, int x, int y);
bool isValid(int x, int y, int rows, int columns);
void findShortestPath(vector<vector<char>> maze, vector<vector<int>> visited, int i, int j, int x, int y, int& min_dist, int dist, string& p_route,string route, int rows, int columns);
void userInput();
void createMaze(int rows, int columns, string filename);



int main()
{
    userInput();
}

bool isSafe(vector<vector<char>> maze, vector<vector<int>> visited, int x, int y)
{
    if (maze[x][y] == 'x' || visited[x][y] || maze[x][y] == NULL)
        return false;

    return true;
}

bool isValid(int x, int y, int rows, int columns)
{
    if (x < rows && y < columns && x >= 0 && y >= 0)
        return true;

    return false;
}

void findShortestPath(vector<vector<char>> maze, vector<vector<int>> visited, int i, int j, int x, int y, int& min_dist, int dist,string&p_route,string route, int rows, int columns)  //I&J Start Point, X&Y End point
{
    bool solved = false;
    // if destination is found, update min_dist
    while (!solved)
    {


        if (i == x && j == y)
        {
            p_route = route;
            min_dist = min(dist, min_dist);
                if (min_dist != INT_MAX)
                {
                 cout << "The shortest path from source to destination has length " << min_dist << endl;
                 cout << "The route through the maze is: " << p_route << endl;

                }
                else
                {
                    cout << "Destination can't be reached from given source";
                }
            solved = true;
            return;
        }

        // set (i, j) cell as visited
        visited[i][j] = 1;

        // go to bottom cell
        if (isValid(i + 1, j, rows, columns) && isSafe(maze, visited, i + 1, j))
            findShortestPath(maze, visited, i + 1, j, x, y, min_dist, dist + 1, p_route, route.append("S") , rows,  columns);

        // go to right cell
        if (isValid(i, j + 1, rows, columns) && isSafe(maze, visited, i, j + 1))
            findShortestPath(maze, visited, i, j + 1, x, y, min_dist, dist + 1, p_route, route.append("E") , rows,  columns);

        // go to top cell
        if (isValid(i - 1, j, rows, columns) && isSafe(maze, visited, i - 1, j))
            findShortestPath(maze, visited, i - 1, j, x, y, min_dist, dist + 1, p_route, route.append("N"),  rows,  columns);

        // go to left cell
        if (isValid(i, j - 1, rows, columns) && isSafe(maze, visited, i, j - 1))
            findShortestPath(maze, visited, i, j - 1, x, y, min_dist, dist + 1, p_route, route.append("W") , rows,  columns);

        // Backtrack - Remove (i, j) from visited mazerix
        visited[i][j] = 0;
    }
}

void userInput()
{
    string filename;
    int rows;
    int columns;
    cout << "Please input the name of your maze!" << endl;
    cin >> filename;
    cout << endl;
    cout << "Please input the amount of rows in your maze!" << endl;
    cin >> rows;
    cout << endl;
    cout << "Please input the amount of columns in your maze!" << endl;
    cin >> columns;
    cout << endl;
    createMaze(rows, columns, filename);


}

void createMaze(int rows, int columns, string filename)
{
    int startRow = 0;
    int startColumn = 0;
    int endRow = 0;
    int endColumn = 0;
    int column = 0;
    vector<vector<char>> maze;
    ifstream input(filename);
    char data = input.get();
    string route;
    string p_route;
    while (!input.eof())
    {
        for (int row = 0; row < rows; row++)
        {
            while (data != '\n' && !input.eof())
            {
                if (data == 'A')
                {
                    startRow = row;
                    startColumn = column;
                }
                if (data == 'B')
                {
                    endRow = row;
                    endColumn = column;
                }
                maze[row][column] = data;
                column++;
                data = input.get();
            }
            column = 0;

            data = input.get();
        }
    }
    input.close();
    cout << "The Maze being solved is: " << endl;
    for (int y = 0; y < rows; y++)
    {
        for (int x = 0; x < columns; x++)
        {
            cout << maze[y][x];
        }
        cout << endl;
    }
    input.close();
    // construct a mazerix to keep track of visited cells
    vector<vector<int>> visited;

    // initially all cells are unvisited
   // memset(visited, 0, sizeof visited);

    int min_dist = INT_MAX;
    findShortestPath(maze, visited, startRow, startColumn, endRow, endColumn, min_dist, 0, p_route, route, rows,columns);
}
  • Some helpful reading to resolve a future bug: [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – user4581301 Dec 01 '20 at 18:08

1 Answers1

1

Inside createMaze you declare the maze:

vector<vector<char>> maze;

This is an empty vector.

Then you attempt to assign to non-existing elements:

 maze[row][column] = data;

As maze is still empty at that point any index is out-of-bounds.

You can create the vector with desired size by calling the appropriate constructor:

auto maze = std::vector<vector<char>>(row,std::vector<char>(columns));

This is causing the out-of-bounds error, but I see more problems in your code. For example at several places you pass the maze by value when a reference would be fine (and perhaps a copy is wrong).

463035818_is_not_a_number
  • 64,173
  • 8
  • 58
  • 126