0

When I run this code:

for (int i = 0; i < indexArray.size(); i++) {

                iss.str(indexArray.at(i));
                iss>>one;
                iss>>two;
                iss>>three;
                cout<<one<<" "<<two<<" "<<" "<<three<<" "<<"\n";
}

the istreamstream (iss) remains the same with every iteration. (The file is read into an vector at the beginning of the program. Yes, I checked to make sure the array had the corresponding data.)

In other words, I get this output:

12345 1  0 
12345 1  0 
12345 1  0 

whereas the file/vector actually says:

12345 1 0
12346 1 25
12543 1 50

I've tried various traces and can't pinpoint the problem. Thanks!

Ester Lin
  • 607
  • 1
  • 6
  • 20

1 Answers1

1

You should call iss.clear(); before iss.str(indexArray.at(i)); to clear the EOF flag when reusing istringstream.

timrau
  • 22,578
  • 4
  • 51
  • 64