If you're trying to reset the stream with each iteration, this will likely do what you seek:
int sum = 0;
stringstream ss("13245");
while (1)
{
int c;
while ((c = ss.get()) != EOF)
sum += c - '0';
ss.clear(); // clear stream state
ss.str(""); // clear buffer
ss << sum; // write new data
if (sum<10 )
break;
sum=0;
}
cout << sum;
Output
6
Note: I took liberty to use the get()
member, as it seemed more appropriate for what you were trying to accomplish. And tested with your 9999
aux sample also produces the expected 9
result. To answer your question, yes, the string buffer is not cleared unless you clear it (which we do above with str("")
).