1

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

1 Answers1

1

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:

r.setRoomId(stoi(datas[0]));
r.setRoomName(datas[1]);
r.setRoomDescription(datas[2]);

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.

Community
  • 1
  • 1
Nikita
  • 5,966
  • 2
  • 23
  • 35
  • so what do i need to do. im really not thinking at the moment. just in panick mode. please i help with this part :/ – Destructor2017 Nov 13 '16 at 20:13
  • @Destructor2017 For example, use `vector>` structure for your `datas`. Put each split line inside datas: `datas.push_back(split_at_commas(myData))`. Access it in `createRooms` as `datas[i][/* Data column index: 0, 1, 2*/]`. – Nikita Nov 13 '16 at 20:17
  • @Destructor2017 Also it's better to redesign your solution to eliminate `static` functions with global state. It's always a pain to support or extend such code. – Nikita Nov 13 '16 at 20:20
  • now how can i access that and create room objects. still not going well. – Destructor2017 Nov 13 '16 at 20:24
  • yea i know i shouldn't be doing that but i just want to see this working. – Destructor2017 Nov 13 '16 at 20:26
  • @Destructor2017 Each line from file transforms into vector. Each such value stored in `datas`, whole file transformed into `vector>`. In `createRooms` you iterate `datas` and from on each iteration create room like: `r.setRoomId(stoi(datas[i][0]));`. – Nikita Nov 13 '16 at 20:34
  • i keep getting this : `Microsoft C++ exception: std::invalid_argument at memory location 0x006FFD60` – Destructor2017 Nov 13 '16 at 20:38
  • @Destructor2017 Debug the program. It should be easy to fix. Do you properly process the last empty string? Does your `split_at_commas` function properly split string into 3 chunks to be able to access `datas[0]`, `datas[1]` and `datas[3]` for each line? – Nikita Nov 13 '16 at 20:44
  • yes i tested that already and it does split the string into 3 different chunks. – Destructor2017 Nov 13 '16 at 20:47
  • @Destructor2017 When the `std::invalid_argument` is thrown? – Nikita Nov 13 '16 at 20:51
  • it finds some just the first line which is the id of one. the crashes – Destructor2017 Nov 13 '16 at 20:58
  • @Destructor2017 It crashes after first line id at `createRooms` or after first line at `loadRooms`? – Nikita Nov 13 '16 at 21:04
  • after the first line at id `createRooms` – Destructor2017 Nov 13 '16 at 21:06
  • i cant i need 20 reputation – Destructor2017 Nov 13 '16 at 21:19
  • @Destructor2017 Ok. Have you changed type of `datas` to `vector>`? – Nikita Nov 13 '16 at 21:21
  • yea i did that but its now crashing at last line. i changed my file a little now its crashing at last line to get `roomDescription` – Destructor2017 Nov 13 '16 at 21:23
  • @Destructor2017 Probably you should post implementation of `setRoomDescription` and `setName`. As I don't see where access violation could occur. – Nikita Nov 13 '16 at 21:26
  • i just eddited the post. i cant see whats wrong with it tbh – Destructor2017 Nov 13 '16 at 21:31
  • @Destructor2017 The only thing I can guess is the vector element access by indexes 1,2,3. vector's `operator[]` doesn't do bound check. It produces undefined results which is copied into string after that in `setXXX` method. Before room creation at `createRooms` on each iteration, check that `datas[i] == 3` and if it's not `continue` to the next iteration. – Nikita Nov 13 '16 at 21:43
  • just did that and now no crash just nothing is printed – Destructor2017 Nov 13 '16 at 21:53
  • @Destructor2017 Great! Please mark the post as an answer to show to the community that the problem resolved. – Nikita Nov 13 '16 at 22:00
  • 1
    Ok just did that. my god this would have been fun if it was java. thank you. was literally going to start crying. hahah – Destructor2017 Nov 13 '16 at 22:03