I am making a console game and I am making a map. The map is an array of vectors. The vectors contain the characters that I am printing to the console. My code:
"Window.h"
#include <string>
#include <vector>
class Row {
public:
std::vector<char> row;
int id;
Row();
void printRow();
void resetRow();
void insertStringIntoRow(std::string toInsert, int startIndex);
std::vector<char> getRow() {
return row;
}
};
class Window {
public:
void showMap();
void writeToMap(std::string stringToInsert, int rowNum, int startIndex);
void writeInRectangle(std::string stringToWrite, int rowNum, int startIndex);
void setCursorToPosition(int x, int y);
void resetMap();
Row getRowAt(int index);
};
void initColors();
void setWindow(Window windowToSet);
Window getGameWindow();
"Window.cpp"
#include "Window.h"
#include <iostream>
#include <Windows.h>
#include <WinBase.h>
#include <stdlib.h>
#include "color.h"
using namespace eku;
Row map[25];
//Class Window
void Window::showMap() {
setCursorToPosition(0, 0);
for (int i = 0; i < 25; i++) {
getRowAt(i).printRow();
}
}
void Window::writeToMap(std::string stringToInsert, int rowNum, int startIndex) {
Row r = getRowAt(rowNum);
r.insertStringIntoRow(stringToInsert, startIndex);
}
void Window::writeInRectangle(std::string stringToWrite, int rowNum, int startIndex) {
if (startIndex != 0) {
std::string topbar = "~";
for (int i = 0; i < stringToWrite.length() + 2; i++) {
topbar += ' ';
}
topbar += '^';
getRowAt(rowNum - 1).insertStringIntoRow(topbar, startIndex - 1);
}
std::string toInsert = "~ ^" + stringToWrite + "~ ^";
getRowAt(rowNum).insertStringIntoRow(toInsert, startIndex - 1);
if (startIndex != 25) {
std::string bottombar = "~";
for (int i = 0; i < stringToWrite.length() + 2; i++) {
bottombar += ' ';
}
bottombar += '^';
getRowAt(rowNum + 1).insertStringIntoRow(bottombar, startIndex - 1);
}
}
void Window::setCursorToPosition(int x, int y) {
HANDLE hOut;
COORD Position;
hOut = GetStdHandle(STD_OUTPUT_HANDLE);
Position.X = x;
Position.Y = y;
SetConsoleCursorPosition(hOut, Position);
}
void Window::resetMap() {
for (Row row : map) {
row.resetRow();
}
}
Row Window::getRowAt(int index)
{
return map[index];
}
//Class Row
const char WHITEBACKCOL = '~';
const char DEFCOL = '^';
int i = 0;
Row::Row() {
row.resize(80, ' ');
id = i;
i++;
}
void Row::printRow() {
for (int i = 0; i < row.size(); i++) {
switch (row[i]) {
case WHITEBACKCOL:
setcolor(black, white);
case DEFCOL:
setcolor(white, black);
default:
std::cout << row[i];
}
}
}
void Row::resetRow() {
row.resize(80);
for (int i = 0; i < 80; i++) {
row[i] = ' ';
}
}
void Row::insertStringIntoRow(std::string toInsert, int startIndex) {
int endIndex = (startIndex + toInsert.length());
int stringPos = 0;
for (int i = startIndex; i < endIndex; i++) {
if (i < row.size() - 1) {
row.at(i) = toInsert[stringPos];
}
else {
row.push_back(toInsert[stringPos]);
}
stringPos++;
}
}
Window defWindow;
void initColors() {
concolinit();
setcolor(white, black);
}
void setWindow(Window windowToSet) {
defWindow = windowToSet;
}
Window getGameWindow() {
return defWindow;
}
"Main.cpp"
#include <iostream>
#include "GameEngine.h"
#include "Window.h"
int main() {
setWindow(Window());
initColors();
getGameWindow().writeInRectangle("Hia", 1, 10);
getGameWindow().showMap();
}
Whenever I call showMap() all I get is a blank console. It seems to be only printing the default map of spaces instead of the text I entered. I also tried using just printRow() to print the single rows that I edited but they also showed only spaces.
I was able view changes to the vector row in the insertStringIntoRow() method but then even though the changes should there they didn't show anywhere else. It almost seems like my Row object is being created every time I access it. I am new to C++ so any help is appreciated. Thanks in advance!
This function is being used incorrectly. Warning signs are that it is non-const, yet used as an accessor. It returns a copy of the vector, not a reference.
std::vector<char> getRow() {
return row;
}
The way you are calling it, you are expecting it to modify the row in-place, but all you are actually doing is modifying a copy of the row, which is then immediately discarded. Example:
getRowAt(rowNum).insertStringIntoRow(toInsert, startIndex - 1);
The easy fix for this behaviour is to return a reference from getRow():
std::vector<char> & getRow() {
return row;
}
The other thing to fix is where you assign this to a temporary variable instead of using it inline. In that case, you can make the temporary a reference:
Row & r = getRowAt(rowNum);
r.insertStringIntoRow(stringToInsert, startIndex);
The correct way to implement this is to also provide a const-version, so that it can still be called on const objects where the caller does not want to modify it.
const std::vector<char> & getRow() const {
return row;
}
I've only put this extra bit in here because it is good practice, but you should not do it in this program. It would clutter your code which is already full of worse practices =)
send a reference to writeToMap() instead of variables.
This will make changes to the original map objects instead of copy-variables.
Related
I wrote a bubble sorting algorithm which sorts a dynamically allocated array using string comparison.
Here is my code:
void AddressBook::bubble_sort_address_book(){
bool swapped = true;
while(swapped){
swapped = false;
for(int i = 0; i < noOfEmployees; i++){
if(employees[i].combined_name() > employees[i+1].combined_name()){
Employee temp_employee = employees[i+1];
employees[i+1] = employees[i];
employees[i] = temp_employee;
}
}
}
}
My problem is pretty obvious, yet I can not seem to figure out how to solve it: The code sometimes fails on the line (in an undefined manner) :
Employee temp_employee = employees[i+1]
Its pretty obvious because if i is equal to the end of the array, accessing memory with i+1 results in undefined behaviour. However, if I stop the for loop with noOfEmployees-1, this does not happen but the first element is never sorted (obviously).
How can I implement bubble sort properly? It seems as such a trivial task. Am I missing something?
The following simplified version in pure C works fine:
int employees[10]= {3,1,7,6,9,7,1,0,2,6};
int noOfEmployees= 10;
void bubble_sort_address_book(void){
bool swapped = true;
int i;
while(swapped){
swapped = false;
for(i = 0; i < noOfEmployees-1; i++){
if(employees[i] > employees[i+1]){
int temp_employee = employees[i+1];
employees[i+1] = employees[i];
employees[i] = temp_employee;
swapped= true;
}
}
}
}
int main()
{
int i;
bubble_sort_address_book();
for (i=0; i<noOfEmployees; i++) {
printf("emp %d= %d\n", i, employees[i]);
}
return 0;
}
As you request, the function of variable swapped is to indicate that following a complete pass through the array no swap occurred and so it indicates the array is now sorted.
You can use an explicit bound on the outer loop.
You should also split things out into smaller functions.
bool operator <(Employee const & lhs, Employee const & rhs) {
return lhs.combined_name() < rhs.combined_name();
}
// a.k.a. std::swap
void swap(Employee & lhs, Employee & rhs) {
Employee temp(static_cast<Employee&&>(lhs)); // a.k.a. std::move
lhs = static_cast<Employee&&>(rhs);
rhs = static_cast<Employee&&>(temp);
}
void bubble_sort_impl(Employee * begin, Employee * end) {
for (; end != begin; --end) {
for (Employee * it = begin; it+1 != end; ++it) {
if (*(it+1) < *it) {
swap(*it, *(it+1));
}
}
}
}
// do we really need "bubble_" or "_address_book" in this name?
void AddressBook::bubble_sort_address_book() {
bubble_sort_impl(employees, employees + noOfEmployees);
}
another solution:
#include <iostream>
#include <vector>
using namespace std;
int employees[10] = { 3,1,7,6,9,7,1,0,2,6 };
void bubble_sort_address_book(void) {
bool swapped = true;
int i;
int noOfEmployees = 10;
while (swapped) {
swapped = false;
for (i = 1; i <= noOfEmployees ; i++) {
if (employees[i] > employees[i - 1]) {
int temp_employee = employees[i - 1];
employees[i - 1] = employees[i];
employees[i] = temp_employee;
swapped = true;
}
}
}
}
int main()
{
int i;
int noOfEmployees = 10;
bubble_sort_address_book();
for (i = 0; i<noOfEmployees; i++) {
printf("emp %d= %d\n", i, employees[i]);
}
return 0;
}
I have problem only with the push_back function, the compiler said:
CRT detected that the application wrote to memory after end of heap buffer
I want to make a push_back function, that adds a new element to the vector's end.
#pragma once
#include <cstdio>
#include <cmath>
#include <iostream>
#include <cstdlib>
class tomb {
private:
double *adat;
int szam;
public:
tomb(){
adat = NULL;
szam = 0;
}
int meret()const {
return szam;
}
~tomb() {
delete[] adat;
}
double & operator[](int n) {
return adat[n];
}
const double & operator[](int n)const {
return adat[n];
}
void push_back(const double &a) {
double *tmp;
int pos = szam + 1;
tmp = new double[szam+1];
for (int i = 0; i < szam; i++)
{
tmp[i] = adat[i];
}
tmp[pos] = a;
delete[] adat;
adat = tmp;
++szam;
}
void Kiir()const {
for (int i = 0; i < szam; i++)
{
std::cout << adat[i] << "\n";
}
}
};
pos should be szam not szam+1. You are willing to insert at the last position, which in 0-based indexing is n-1.
The problem is in this line:
tmp[pos] = a;
Since pos is initialized to szam + 1, that is equivalent to:
tmp[szam + 1] = a;
which is one out of the array limit.
The solution is to get rid of pos altogether and just do:
tmp[szam] = a;
BTW, your class is using the default copy constructor and assignment operator, and those will not work properly. You should really do something about that.
in my C++ class we are finally getting conceptually fairly deep (well, relatively!) and I'm struggling with building a class from a previous class.
Here is my first class header, which builds partially filled array objects. To my knowledge, it is fully functional:
#ifndef PARTIALARRAY_H
#define PARTIALARRAY_H
#include <iostream>
#include <string.h>
using namespace std;
typedef int ITEM_TYPE;
ITEM_TYPE const MAX = 50;
class PartialArray
{
public:
//-----------------------------------------ctors:-----------------------------------------
PartialArray();
PartialArray(const int init[], int used);
//-----------------------------------------member functions:-----------------------------------------
void PrintArray();
int Search(ITEM_TYPE key);
int Append(ITEM_TYPE appendMe);
int ShiftRight(int shiftHere);
int ShiftLeft(int shiftHere);
int InsertBefore(ITEM_TYPE insertThis, int insertHere);
int InsertAfter(ITEM_TYPE insertThis, int insertHere);
int Delete(int deleteHere);
void DeleteRepeats();
int NumUsed();
void Sort();
void Reverse();
string ErrorDescr(int failCode);
//-----------------------------------------operators:-----------------------------------------
ITEM_TYPE& operator [] (ITEM_TYPE x);
private:
//-----------------------------------------member vars:-----------------------------------------
ITEM_TYPE a[MAX];
int numUsed;
};
#endif // PARTIALARRAY_H
And here are the class functions:
#include "partialarray.h"
#include <iostream>
#include <string.h>
using namespace std;
//-----------------------------------------ctors:-----------------------------------------
PartialArray::PartialArray()
{
numUsed=0;
}
PartialArray::PartialArray(const int init[], int used)
{
numUsed = used;
for(int i=0; i<numUsed; i++)
{
a[i]=init[i];
}
}
//-----------------------------------------member functions:-----------------------------------------
//Prints the array up to its last used element
void PartialArray::PrintArray()
{
for(int i=0; i<numUsed; i++)
{
cout << a[i] << " ";
}
cout << endl;
}
//Searches the array for a particular value and returns the index at which the value first appears
int PartialArray::Search(ITEM_TYPE key)
{
for(int i=0; i<numUsed; i++)
{
if(a[i]==key)
return i;
}
return -1;
}
//Takes a number and appends it to the end of the array after the last interesting element
int PartialArray::Append(ITEM_TYPE appendMe)
{
if(numUsed<MAX)
a[numUsed++] = appendMe;
else
return 1;
return 0;
}
//Shifts all elements of the array to the right starting at a particular index
int PartialArray::ShiftRight(int shiftHere)
{
if(shiftHere<numUsed)
{
ITEM_TYPE save = a[numUsed-1];
for(int i=numUsed; i>=shiftHere; i--)
{
a[i] = a[i-1];
}
a[shiftHere] = save;
return 0;
}
else
return 2;
}
//Shifts all elements of the array to the left starting at a particular index
int PartialArray::ShiftLeft(int shiftHere)
{
if(shiftHere<numUsed)
{
ITEM_TYPE save = a[shiftHere];
for(int i=shiftHere; i<numUsed; i++)
{
a[i] = a[i+1];
}
a[numUsed-1] = save;
return 0;
}
else
return 2;
}
//Takes a number and a position and inserts the number before that position in the array shifting the elements to the right
int PartialArray::InsertBefore(ITEM_TYPE insertThis, int insertHere)
{
if(insertHere>numUsed)
return 2;
else
{
numUsed++;
ShiftRight(insertHere);
a[insertHere] = insertThis;
}
return 0;
}
//Takes a number and a position and inserts the number after that position in the array shifting the elements to the right
int PartialArray::InsertAfter(ITEM_TYPE insertThis, int insertHere)
{
if(insertHere>numUsed)
return 2;
else if(numUsed>=MAX)
return 1;
else
{
numUsed++;
ShiftRight(insertHere+1);
a[insertHere+1] = insertThis;
}
return 0;
}
//Takes a position and removes that item from the array, shifting all the elements to the left
int PartialArray::Delete(int deleteHere)
{
if(deleteHere <= numUsed)
{
ShiftLeft(deleteHere);
numUsed--;
return 0;
}
else
return 2;
}
//Deletes repeated elements in the array and replaces the with 0
void PartialArray::DeleteRepeats()
{
for(int i=0;i<numUsed;i++)
{
ITEM_TYPE n=a[i];
for(int j=i+1; j<numUsed;j++)
{
if(n == a[j])
{
Delete(j);
j--;
}
}
}
}
//Returns number of interesting elements in the array
int PartialArray::NumUsed()
{
return numUsed;
}
//Utilizes a bubble sort algorithm
void PartialArray::Sort()
{
bool swap = true;
int j = 0;
int save;
while (swap==true)
{
swap = false;
j++;
for (int i = 0; i < numUsed - j; i++)
{
if (a[i] > a[i + 1])
{
save = a[i];
a[i] = a[i + 1];
a[i + 1] = save;
swap = true;
}
}
}
}
void PartialArray::Reverse()
{
for(int i=0;i<numUsed-1;i++)
{
ITEM_TYPE save = a[numUsed-1];
ShiftRight(i);
a[i] = save;
}
}
//Returns the appropriate error description for a particular fail code
string PartialArray::ErrorDescr(int failCode)
{
switch(failCode)
{
case -1:
return "ERROR: item not found";
break;
case 1:
return "ERROR: array is full";
break;
case 2:
return "ERROR: unused index";
break;
default:
return "UNKNOWN ERROR";
break;
}
}
//-----------------------------------------operators:-----------------------------------------
ITEM_TYPE& PartialArray::operator [](ITEM_TYPE x)
{
return a[x];
}
Now, here is where things have gotten tricky. To build the two dimensional array class, I'm supposed to create an array of arrays. I'm at a loss as to how I should go about this, and after tinkering and googling for a few hours I've only become more confused. Specifically, the <<, [], and [](constant version) operators and the TwoDArray constructor have thrown me for a loop, and I'm stuck without much sense of what to do next. Here is the TwoD header file:
#ifndef TWODARRAY_H
#define TWODARRAY_H
#include "partialarray.h"
#include <iostream>
#include <string.h>
typedef int ITEM_TYPE;
class TwoDArray
{
friend ostream& operator << (ostream &outs, const TwoDArray& printMe);
public:
//ctors:
TwoDArray();
//member functions:
//PartialArray& operator [](int index); //[ ] operator for the TwoDArray object
//PartialArray operator [](int index) const; //[ ] operator for the TwoDArray object (const version)
int Append(int appendMe, int row);
int InsertBefore(int insertMe, int row, int column);
int InsertAfter(int insertMe, int row, int column);
int Delete(int row, int column);
bool Search(ITEM_TYPE key, int &row, int &column);
private:
//member vars:
PartialArray a[MAX];
};
#endif // TWODARRAY_H
And this is what I've tried to define thus far:
TwoDArray::TwoDArray()
{
const int array0[]= {0};
PartialArray array(array0, MAX);
}
ostream& operator << (ostream &outs, const TwoDArray& printMe)
{
for(int i=0;i<MAX;i++)
{
outs << printMe.a[i];
}
return outs;
}
Ideally, the << operator will print an m by n array of items.
I'm doing a project with C++ and my program keeps crashing down when I try to run it...here is my code (two files: main.cpp and PlacementHead.cpp):
main.cpp:
#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
#include <ctime>
#include <climits>
#include "PlacementHead.h"
// Main
int main (int argc, char * const argv[]) {
PlacementHead h1(4,2,1,"aabcbbca");
PlacementHead h2(4,2,1,"babcabca");
return 0;
}
PlacementHead.cpp:
#include "PlacementHead.h"
#include <string>
#include <iostream>
#include <string.h>
PlacementHead::PlacementHead(int width, int height, int gap, char* s) {
width_ = width;
height_ = height;
gap_ = gap;
size_ = width*height;
set_ = new char[size_];
from_ = new int[size_];
original_ = new char[size_];
strcpy(set_,s);
strcpy(original_,s);
}
PlacementHead::~PlacementHead() {
}
int PlacementHead::getSize() { return size_; }
int PlacementHead::getHeight() { return height_; }
int PlacementHead::getWidth() { return width_; }
int PlacementHead::getGap() { return gap_; }
char PlacementHead::getNozzle(int i) {
return set_[i-1];
}
void PlacementHead::setNozzle(int i, char c) {
set_[i-1] = c;
}
void PlacementHead::markNozzle(int i, int bankPos) {
set_[i-1] = ' ';
from_[i-1] = bankPos;
}
int PlacementHead::getNextUnmarkedPos() {
for (int i=0; i<size_; i++) {
if (set_[i]!=' ') {
return i+1;
}
}
return 0;
}
int PlacementHead::getBankPos(int i) {
return from_[i-1];
}
void PlacementHead::reset() {
//for (int i=0; i<size_; i++) {
// set_[i] = original_[i];
//}
strcpy(set_,original_);
}
void PlacementHead::print() {
std::cout << "placementhead:\n";
for (int h=height_; h>0; h--) {
for (int w=width_; w>0; w--) {
int i = ((h-1)*width_)+w;
std::cout << getNozzle(i);
}
std::cout << "\n";
}
}
If I try to run the main.cpp, I get this:
Once I got this also (I don't get this every time, which bugs me...):
Now here is also one thing to take into account: If I comment out the second line where PlacementHead h2-object is created the code runs okay, but IF I create more than one PlacementHead-objects the program crashes again...
Any advices what might be causing this?
Thank you for any help!! =)
P.S.
My platform is Windows 7, Codeblocks 12.11 and GNU GCC Compiler
UPDATE:
In case you couldn't see the text on the second picture here it is:
You should
increase size_ by +1 too avoid buffer overrun (or, use std::vector instead, see below)
disable copy construction/assignment
add a proper destructor
use initialization lists
check the order in which members are declared (since this is also the order in which members are initialized!)
remove the default constructor, since it doesn't initialize a single member
include <cstring> instead of <string.h> on modern compilers (so you get namespaces C standard library functions)
Consider using std::vector instead of manual arrays. This will save you much trouble. Think of how you are going to get your constructor exception safe?
Here is a sample fixing all of the above:
#include <iostream>
#include <vector>
#include <cstring>
struct PlacementHead {
int width_, height_, gap_;
size_t size_;
std::vector<char> set_, original_;
std::vector<int> from_;
PlacementHead(int width, int height, int gap, const char* s) :
width_(width),
height_(height),
gap_(gap),
size_(width * height),
set_(s, s + std::min(strlen(s), size_)),
original_(set_),
from_(size_)
{
set_.resize(size_);
original_.resize(size_);
}
size_t getSize() { return size_; }
int getHeight() { return height_; }
int getWidth() { return width_; }
int getGap() { return gap_; }
char getNozzle(int i) { return set_[i - 1]; }
void setNozzle(int i, char c) { set_[i - 1] = c; }
void markNozzle(int i, int bankPos) {
set_[i - 1] = ' ';
from_[i - 1] = bankPos;
}
int getNextUnmarkedPos() {
for(unsigned i = 0; i < size_; i++) {
if(set_[i] != ' ') {
return i + 1;
}
}
return 0;
}
int getBankPos(int i) { return from_[i - 1]; }
void reset() {
//for (int i=0; i<size_; i++) {
// set_[i] = original_[i];
//}
set_ = original_;
}
void print() {
std::cout << "placementhead:\n";
for(int h = height_; h > 0; h--) {
for(int w = width_; w > 0; w--) {
int i = ((h - 1) * width_) + w;
std::cout << getNozzle(i);
}
std::cout << "\n";
}
}
};
// Main
int main (int argc, char * const argv[]) {
PlacementHead h1(4,2,1,"aabcbbca");
PlacementHead h2(4,2,1,"babcabca");
return 0;
}
size_ = width*height; should be size_ = (width*height)+1; so the string can be null terminated. Currently you are writing to unallocated memory causing undefined behaviour
You have an off-by-one error in your constructor, as the strings you try to copy are not 8 characters, they are 9. The reason is that all string literals are also containing an extra special string termination character.
If you're using strings in C++, use std::string, it will help you tremendously with problems like these.
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
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
In your PrintRoom() function, you have
cout << data[col * height + row];
instead of
cout << data[row * height + col];