C
Christopher
This question is asked the world over. I know I've asked it myself,
forgotten the answer and then asked it again. My colleagues ask it all
the time as well. I'd even request that this be added to the FAQ if it
isn't already. I looked it over and didn't find my answer there.
Anyway, question is:
How do you empty out and reuse an iostream inside a loop construct?
I know it was some magical combination of clear, flush, or
ostream::str(""); is some special order, but can't get it right again.
Example:
#include <iostream>
#include <sstream>
int main()
{
std:stringstream os;
for(int i = 0; i < 5; i++)
{
// How to empty and reuse here?
os << i;
std::cout << "you are in iteration #" << os.str() << std::endl;
// Or is it done here?
}
return 0;
}
forgotten the answer and then asked it again. My colleagues ask it all
the time as well. I'd even request that this be added to the FAQ if it
isn't already. I looked it over and didn't find my answer there.
Anyway, question is:
How do you empty out and reuse an iostream inside a loop construct?
I know it was some magical combination of clear, flush, or
ostream::str(""); is some special order, but can't get it right again.
Example:
#include <iostream>
#include <sstream>
int main()
{
std:stringstream os;
for(int i = 0; i < 5; i++)
{
// How to empty and reuse here?
os << i;
std::cout << "you are in iteration #" << os.str() << std::endl;
// Or is it done here?
}
return 0;
}