0

I need to create a program for class that inputs a file of a Maze into a two dimensional vector so the computer can return the vector to a class Maze. I'm currently working on the function that loads the Maze input into a vector. Every time I run the program it stops at the first conditional statement that is meant to check that each line is equal. It "couts" Error and then get abort error from MVS studio saying string subscript out of bounds. I've been searching online for possible errors but nothing has helped. Can someone please tell me what I am doing wrong?

Below is the code:

#include <iostream>
#include <string>
#include <fstream>
#include <vector>

using namespace std;

struct Location  {
    friend std::ostream &operator <<(std::ostream &os, const Location &location) {
        os << "(" << location.x << ", " << location.y << ")";
        return os;
    }
    //bool operator ==(const Location &rhs) const {return x == rhs.x && y == rhs.y;}
    //bool operator !=(const Location &rhs) const {return !(*this == rhs);}
    //operator bool() const {return x >= 0;}
    Location(int x=-1, int y=-1) : x(x), y(y) {}
    int x, y;
};

int main()
{

    ifstream infile;
    infile.open("Maze.txt");

    vector<vector<bool> > mazeSpec;   //row vector
    vector<bool> column;
    string top,currlin;
    int row=0, col=0;
    Location start, finish;

    getline(infile, top);
    int size= top.length();
    for(int i=0; i<size; i++){
        column.push_back(false);
    }
    cout<<top<<endl;
    cout<<top.length();
    //getline(infile,currlin);
    //cout<<(currlin);
    //cout<<currlin.length();

    mazeSpec.push_back(column);
    column.clear();
    row++;

    while (getline(infile,currlin)){

        for(int i=0; i<size; i++){

            if ((currlin.length()) != size){
                    //throw "Error! Line of Maze is too short!"; //check to see if rows of equal length
                cout<<"Error!";}
                    //break;}

            else if (currlin[i] == 'S'){
                    column.push_back(true);
                    start= (row, i);}

            else if (currlin[i] == 'F'){
                    column.push_back(true);
                    finish= (row, i);}

            else if (currlin[i] == ' ' || '+' || '|'){
                    column.push_back(false);
            }

            else if(currlin[i]== '*'){
                column.push_back(true);}
        }
            row++;
            currlin.empty();
            mazeSpec.push_back(column);
            column.clear();
    }





    infile.close();

    int x;
    cin>>x;
    cin.ignore();

    return 0;
}

Here is the Maze text:

+----------------------------------------+
|*                           F           |
|*      ***** *********      *           |
|****   *   * *       *      *           |
|   *   *   * *       ********           |
| ***   *   * *       *  *               |
| *     *   * *       *  *               |
| *******   * *       *                  |
|   *    **** *       *   *         *****|
|   *    *    *       *   *             *|
|   *    ******       ********          *|
|   *                        *          *|
|   S                 *******************|
+----------------------------------------+

Thanks!

Tammytee
  • 592
  • 5
  • 9
  • So I just put a print statement right after the while loop begins, before I get the error message and it shows that getline is reading in the entire maze, NOT one line, so the array goes out of bounds. How can I fix it so that getline reads till the end of the line, I tried adding '\n' to getline but that didn't either help. – Tammytee May 01 '14 at 02:41
  • getline() shouldn't read your entire maze if each row is separated by a newline character. Are you sure about this? – o_weisman May 01 '14 at 07:59
  • yup I know that's why I can't figure out the error – Tammytee May 01 '14 at 15:30
  • actually it reads in line by line but it nevers enter the forloop like its supposed to before reading in the next line – Tammytee May 01 '14 at 15:32
  • You're using the returned stream as your condition, I don't really know how it is evaluated. You could try to check the eof flag of the stream in your while instead. – o_weisman May 01 '14 at 16:24
  • Apparently you're allowed to use the return value of getline() as a condition (although the c++ reference I read shows it returns a stream reference). I don't see any reason for the inner for loop not to be executed unless size <= 0 . You should use a debugger to see what is really being executed and what is the value of size. Also, the line: `if ((currlin.length()) != size)` should probably be moved before the for loop. – o_weisman May 04 '14 at 05:51

0 Answers0