samedi 25 avril 2015

while(getline(myReadFile, temp, ':')) executing one iteration too many causing out of bounds on vector


I have a question on std::readline(istream, string, delimiter). I am trying to read in a file and store the data in a struct and add it to a map. That I have gotten to work. However my while loop iterates one loop too many causing my vector to have no data stored in it which causes an assertion failure for out of bounds.

I have read every stack overflow question on readline and none seem to give me any idea as to why this behavior occurs. Perhaps someone here can enlighten me.

if (myReadFile.is_open()) {
    while(getline(myReadFile, temp, ':')){//loops through and splits the line one delimiter at a time
        stringVector.push_back(temp);//add to vector
        ItemInfo tempItem = ItemInfo(stringVector[1], stod(stringVector[2]), stod(stringVector[3]));//create the struct
        catalog.insert(pair<string,ItemInfo>(stringVector[0], tempItem));//add to map
        stringVector.clear();
    }

}

    myReadFile.close();

Thanks for any help


Aucun commentaire:

Enregistrer un commentaire