1

Possible Duplicate:
In C++, how do you clear a stringstream variable?

I have a problem with a string i want to use to display in my loop.

I set it up like this in my loop:

//while {
 std::stringstream s;
 s << "Hello";

  font_surface = TTF_RenderText_Solid(font,s.str().c_str(),font_color);
  apply_surface(x,y,font_surface,screen);   

  s << "GoodBye";
  if(font_surface = TTF_RenderText_Solid(font,s.str().c_str(),font_color);
  apply_surface(bx,by,font_surface,screen);


//end loop }

The problem i have is the output first shows Hello then in the other location it has HelloGoodBye, i need to clear the content before i add Goodbye so i only see that in the second location on screen.

So how would i clear the information of Hello before I change it to Goodbye ?

Community
  • 1
  • 1
Sir
  • 8,135
  • 17
  • 83
  • 146
  • It seems like using a `std::string` would be a better option. `std::string s = "Hello"; /*call functions*/ s = "Goodbye"; /*call functions*/` – chris Oct 28 '12 at 04:38
  • I was suggested the current method i use because i will add variables to my strings for output which will be holding ints. – Sir Oct 28 '12 at 04:39
  • Ah, I see. You can use `s.str("");`, then. See this: http://stackoverflow.com/questions/20731/in-c-how-do-you-clear-a-stringstream-variable – chris Oct 28 '12 at 04:40
  • Works a charm thanks! If you want answer the Q and I will tick it for rep :) – Sir Oct 28 '12 at 04:40
  • It's probably better as a dupe, seeing as how the question does boil down to exactly the other's title. – chris Oct 28 '12 at 04:41

1 Answers1

3

If you must use a stringstream, call

s.str("");

to clear it

sampson-chen
  • 45,805
  • 12
  • 84
  • 81