Ester Lin
Ester Lin

Reputation: 617

Re-setting an istringstream object

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!

Upvotes: 0

Views: 51

Answers (1)

timrau
timrau

Reputation: 23058

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

Upvotes: 1

Related Questions