2012-10-28 29 views
1

可能重複:
In C++, how do you clear a stringstream variable?更改可變文本

我有一個字符串,我想用我的循環來顯示一個問題。

我把它像這樣在我的循環:

//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 } 

我的問題是輸出第一顯示Hello然後在它HelloGoodBye的其他位置,我需要我添加Goodbye之前清除內容所以我只能看到屏幕上的第二個位置。

那麼在將其更改爲Goodbye之前,如何清除Hello的信息?

+0

這似乎是使用'的std :: string'會是一個更好的選擇。 'std :: string s =「Hello」;/*通話功能*/s =「再見」;/*調用函數* /' – chris

+0

我被建議使用當前的方法,因爲我將添加變量到我的字符串輸出將保存整數。 – Sir

+0

啊,我明白了。你可以使用's.str(「」);',然後。看到這個:http://stackoverflow.com/questions/20731/in-c-how-do-you-clear-a-stringstream-variable – chris

回答

3

如果你必須使用一個字符串流,調用

s.str(""); 

將其清除