So I have a data file that looks like this

Movie1
Year Salary
Actor1, Actor2
Movie2
Year Salary
Actor3, Actor4, Actor5
... etc

The part that's throwing me off is the actors line. It can have from 1 to 5 actors on it, separated by commas. I have to store each actor as a string into an array. This is what I have so far:

void Set_Actors(ifstream &fin){
     for(int i=0;i<5;i++){
     getline(fin, Actors[i], ',');
     fin.ignore(1,' ');
     }
}

Of course that isn't working because it stores 5 strings, but that number is different depending on the data file. So what's a better way to do it?

Use a std::stringstream:

void Set_Actors(ifstream &fin){
     string s;
     getline(fin, s);
     istringstream ss(s);
     for (unsigned n=0; getline(ss, Actors[n], ','); n++){
          ss >> ws;
     }
}

Notice how I use >> ws instead of ignore(...) ... it is more robust that way.

Hope this helps.

This question has already been answered. Start a new discussion instead.