Destructor2017 Destructor2017 - 2 months ago 9
C++ Question

Reading from test File of (csv) and storing into room object

So im trying to read from a text file values are seperated with commas and then i want to store this into a vector. Im new with c++ and i am having a difficult time getting used to it. so far I what i did was read the file, store in

and then split the data in that vector where its seperated by commas, Then i try to loop through and create
objects with the string value positions. I dont know what im doing wrong but whenever i try to print nothing appears on cmd line. i dont know what im doing wrong. is there another way ? and why isnt it printing in command. Here is what i did so far:

vector<string> split_at_commas(const string& row)
vector<string> res;
istringstream buf(row);
string s;
while (getline(buf, s, ','))
return res;

static void loadRooms() {
ifstream input("room.txt"); //Go find the file
if (input.is_open()) {

while (!input.eof()) {
string fileData;
string myData;
getline(input, fileData); //read fileData
myData = fileData; //now fileData is myData

datas = split_at_commas(myData);
//string iddata = dataFromFile[0]
//string name = dataFromFile[1];
//string description = dataFromFile[2];
//int id = std::stoi(iddata);
//for each (string var in datas)
// cout << var << endl; //print it out

//cout << datas << endl; //print it out


static void createRooms() {
for (size_t i = 0; i < datas.size(); i++) {
Room r;


int main() { //Application Interaction

/* giving users options and based on the enetered input an action is executed
I decided to use numbers for users options intead
cout << "Chose what to do Next(Enter Number) \n";
cout << "1. Explore your current room (Enter 1) \n";
cout << "2. Process to North (Enter 2) \n";


working sample of input file :

1,Generator Room, This is the starting line. welcome to ZombieScare where it gets real. Your at the starting line
and you've only got one option which is to turn proceed to the north of here.But dont forget to explore this room.

2, Activate power, You are in a room north of the room you started from and your presented with 2 electrical doors.
You need to turn on the power in this room... Lucky for you your next to the power generator. ACTIVATE THE POWER GENERATOR!

3, Robotic Arival, you sucessfully activated power and chose to proceed to the eastern room.. SUPRISE The Amoured Robot Zombie Spawned!!
What to do?

implementation of

void Room::setRoomId(int id)
roomId = id;

void Room::setRoomName(string rmName)
roomName = rmName;

void Room::setRoomDescription(string rmDesc)
roomDescription = rmDesc;

Please any help would be great. I don't know whats wrong with this. Thank you


On each iteration you overwrite your datas. It means, at createRooms, rooms created only from the last split values.

Probably you have the new line at the last string of a file. On the last iteration, myData become an empty string and you got the empty datas vector. Finally nothing is printed to the console.

Access datas values inside the loop at createRooms is suspicious:


Why you are iterating by i and accessing by 0, 1, 2? It looks like you expect here vector<vector<string>> to keep each split result.

By the way, condition:

while (!input.eof())

is bad approach to finish file reading.