So I have a data file that looks like this

Year Salary
Actor1, Actor2
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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.