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 std::vector<string>
and then split the data in that vector where its seperated by commas, Then i try to loop through and create Room
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, ','))
res.push_back(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;
r.setRoomId(stoi(datas[0]));
r.setRoomName(datas[1]);
r.setRoomDescription(datas[2]);
room.push_back(r);
}
}
int main() { //Application Interaction
loadRooms();
createRooms();
/* 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";
system("PAUSE");
}
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 setRoom
setName
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