I am trying to write a "text adventure" engine that runs off of text and data files. I understand the concepts of arrays and multidimensional arrays, but know they cause memory leaks and not re-sizable. Therefor I have decided to use vectors and vectors of vectors (or multidimensional vectors). I have a data file that holds at current time 52 lines, each line has 10 integers separated by a space. (for this board) I cut the data file down to 3 lines. I'm having a terrible time reading the data into a multi-d vector. My question is what am I doing wrong? Thank you in advance for any insights.
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
int main()
{
vector<vector <int> > rooms;
vector <int> locations;
ifstream openfile("rooms.dat");
if (openfile.is_open()){
while (!openfile.eof()){
int temproom; openfile >> temproom;
locations.push_back(temproom);
}
rooms.push_back(locations);
locations.clear();
}
for (int y=0;y<3;y++){
for ( int i=0; i<9; i++){
cout << rooms [y][ i ] << " , " ;
}
cout << rooms[y][9] << "\n" << endl;
}
return 0;
}
I have a data file that looks like:
23,6,4,8,2,12,6,3,2,6
65,34,22,65,34,23,11,5,3,3
3,12,21,4,9,5,2,7,43,22