Empty stringstream/string

Mar 2, 2015 at 6:12pm
Hi guys

I'm facing some Trouble again... I want to print the score to the Screen, but it wont work because the max. texturesize is overstepped. The score texture will only update if something has changed and i set the "NeedsUpdate"-flag to true. This works properly :)

The Problem i'm encountering is that the stringstream seems to be setting the Score-value over and over :/ So in each loop the score will be added at the end.

First run in console output: 000000
Second run in console: 000000000000
Third run in console: 000000000000000000
Fourth run in console: 000000000000000000000000

The same happens with the Lives and Coins… How can I empty the stringstream / the string so it wont just add the new number at the end? 
1
2
3
4
5
6
7
8
9
10
11
12
13
void CPlayer::UpdateScore()
{
    if (m_Score <= 0)
    {
        m_Score = 0;
    }

    ss.clear();
    ss << std::setw(6) << std::setfill('0') << m_Score;
    m_sScore = "";
    m_sScore = ss.str().c_str();
    std::cout << "m_sScore: " << m_sScore << "\n";
}


The ss.clear() and m_sScore = "" didnt bring any changes at all :/

Thanks for your help
Last edited on Mar 2, 2015 at 6:12pm
Mar 2, 2015 at 6:57pm
ss.clear() clears the error flags (not what you want). To reset the content of the stream you can do ss.str("");. Another option is to declare the stringstream locally inside the UpdateScore() function, then you don't need to reset it because it will be created fresh every time the function is called.
Last edited on Mar 2, 2015 at 6:59pm
Mar 2, 2015 at 7:37pm
Hi Peter :D

Well I'll try out what you told me in a sec. If you allow another question: which way is more efficient? (I know it probably doesn’t really matter much, just for learning purposes)

Thanks
Mar 2, 2015 at 8:07pm
If UpdateScore will be called frequently (as in "dozens of times per second") then it is better to make ss either class field or static (or even better, thread_local) local method variable, as in all common implementations its constructor is painfully slow. Reusing it by calling str("") is usually faster.
Mar 2, 2015 at 8:59pm
Thanks :D
Topic archived. No new replies allowed.