-1

I'm trying to use ostringstream to concatenate a string in a loop. Unfortunately, only the most recent stream is used. Can anyone point me in the right direction on accomplishing this or similar (other the + with string concatenation)?

Thanks

std:ostringstream os;
for (int i = stk.pop(); i != 0; i = stk.pop()) {
    os << i << endl;
}
cout os.str();

So the value of 'os' is overwritten every time? Is there a way to append to the stream?

SOLUTION This code works, I had a bug.

Thanks

1 Answers1

1

That code shouldn't be overwriting os. It should be appending to it using the << operator. Perhaps your bug is elsewhere? Maybe your stack only really has the last element?

GaryO
  • 5,873
  • 1
  • 36
  • 61
  • This makes a decent comment, but cannot, because the information provided in the question is so limited, provide an answer to the implied question, "Why the doesn't my code work?" – user4581301 May 17 '19 at 19:45
  • Point taken. We'll have to see -- maybe I'll get lucky and guess his bug. ;-) – GaryO May 17 '19 at 22:16