-
help with loop condition
can someone help me, this little demo program (to convert string to stream) seems to be stuck in the while loop
can someone give me a suggestion
objective to loop though a string of varying lengths and separate out the words by the commas
Code:
#include <iostream.h>
#include <stdlib.h>
#include <string>
#include "sstream.h"
int main()
{
string s="John Jones, Day, Night,";
string temp;
istringstream iss(s);
while(true)
{
getline(iss, temp,',');
if(temp.size()==0) break;
cout << temp <<" ";
}
system("PAUSE");
return 0;
}
-
Im not sure because I dont use those functions, but an infinite loop could be caused if the getline function doesnt set the string 'temp' to "" when there is on more information in the stream, i.e. temp retains the value from the previous call. A possible remedy would be to test whether the stream itself is still valid. How about trying iss.eof(), iss.fail(), iss.good(), !iss -- whichever one works.