Question

I'm a C++ noob, currently trying to create a text based maze game for degree. I'm replacing my currently working array with a vector (for dynamic size) and reading a txt file of chars to it.

PROBLEM: I get no IDE errors, but get RUNTIME ERROR: Debug assertion Failed! Expression: vector subscript out of range!

I have used the pushback instead of RoomFile=data, but I don't know if I need to amend other functions. I read the array/vector data in another class (player) and will include code if needed.

I have searched everywhere to find solution, but can't find any projects using a similar vector structure. What am I doing wrong?

Room.h

#pragma once
#include <vector>
#include "stdafx.h"

class Room
{
private:        //data acceessed by functions

    char floor;
    char wall;
    char door;
    int width;
    int height;

public: 

    std::vector <char> data;

    Room* North;
    Room* East;
    Room* South;
    Room* West;
    Room* Next;

    int NorthX;
    int NorthY;
    int EastX;
    int EastY;
    int SouthX;
    int SouthY;
    int WestX;
    int WestY;

    char RoomFile;


                //where functions go
    Room(void);
    void CreateRoom(int RoomNo);
    void PrintRoom();
    ~Room(void);

};

Room.cpp

#include <iostream>
#include <fstream>
#include <streambuf>
#include <string>
#include <sstream>
#include "Room.h"
#include <cstdlib>


using namespace std;

Room::Room(void)
{
floor = ' ';
wall = '#';
door = 'X';

width = 11;
height = 11;

North=0;
East=0;
South=0;
West=0;
Next=0;
NorthX=0;
NorthY=0;
EastX = 0;
EastY = 0;
SouthX= 0;
SouthY=0;
WestX=0;
WestY=0;

}

void Room::CreateRoom(int RoomNo)
{
std::vector<char> data;

char RoomFile[255];
ifstream infile;
stringstream ss;

//LOAD CURRENT ROOM FROM FILE

ss << RoomNo;
string str = ss.str();

string fname = ("Room");
fname.append(str);
fname.append(".txt");
infile.open(fname);

infile.clear();
infile.seekg(0);

if(infile.is_open())
{
    while(!infile.eof())        // To get you all the lines.
    {
        int i;
        for(int row = 0; row < width; row++)
        {
            infile.getline(RoomFile, 256);
            i = 0;
            for(int col = 0; col < height; col++)
            {
                data.push_back(RoomFile[i]);
                i++;
            }
        }

    }
}
else
{
    cout << "ERROR: infile not open" << endl; 
}
infile.close();

//ASSIGN DOORS TO ROOMS IF NEEDED

// treating the array as being one-dimensional.
for(int row = 0; row < width; row++)
{
    for(int col = 0; col < height; col++)
    {
        if(North!=NULL)
        {
            if(data[row*height+col]=='X') 
            {
                //data[row*height+col] = door;
                NorthX=row;                     
                NorthY=col;
            }
        }

        if(East!=NULL)
        {
            if(data[row*height+col]=='X') 
            {
                //data[row*height+col] = door;
                EastX = row;
                EastY = col;
            }
        }

        if(South!=NULL)
        {
            if(data[row*height+col]=='X') 
            {
                //data[row*height+col] = door;
                SouthX = row;
                SouthY = col;
            }
        }

        if(West!=NULL)
        {
            if(data[row*height+col]=='X') 
            {
                //data[row*height+col] = door;
                WestX = row;
                WestY = col;
            }
        }

    }
}
}

void Room::PrintRoom()
{

for(int row = 0; row < width; row++)
{

    for(int col = 0; col < height; col++)
    {

        cout << data[col * height + row];
    }
    cout << "\n";                           
}
cout << endl;
}

Room::~Room(void)
{

}

Stack Trace

// throw -- terminate on thrown exception REPLACEABLE
#define _HAS_EXCEPTIONS 0
#include <cstdio>
#include <cstdlib>
#include <exception>
#include <crtdbg.h>

_STD_BEGIN

#ifdef _DEBUG
_CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL _Debug_message(const wchar_t *message, const wchar_t *file, unsigned int line)
{   // report error and die
    if(::_CrtDbgReportW(_CRT_ASSERT, file, line, NULL, L"%s", message)==1)
    {
        ::_CrtDbgBreak();
    }
}
_CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL _Debug_message(const unsigned short *message, const unsigned short *file, unsigned int line)
{   // report error and die
    _Debug_message((wchar_t *) message, (wchar_t *) file, line);
}

#endif

_STD_END
Was it helpful?

Solution

I think this is the error: What you are printing in PrintRoom is the std::vector<char> data inside your class, while the std::vector<char> data that you filled up is local to CreateRoom method.

void Room::CreateRoom(int RoomNo)
{
std::vector<char> data; // remove this to solve

OTHER TIPS

In your PrintRoom() function, you have

cout << data[col * height + row];

instead of

cout << data[row * height + col];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top