stringstream doing funny things in for loop (using SFML)

Hi everyone,

I'm getting funny results using stringstream within a for loop.

I have 2 for loops looping through a 2D vector of Tiles (I'm making a tile engine for a game). I want to display a string over each tile that shows its 2d vector indexes. Since c++ and SFML don't have a simple method of concatenating strings with other data types (like fprint) I have to use a stringstream and set that as my sf::String text.

Scenario 1 works the way I'm intending: Setting sf::String's text to "test" without using stringstream. This displays 1 word "test" over each tile.

Scenaria 2's result is unexpected: Setting stringstream to "test" and setting sf::String's text to stringstream.str() display 1 "test" on the first tile, 2 "test"s on the second, 3 on the third, etc until it's all a jumbled mess.

Here's the [simplified] code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
std::stringstream ss;
sf::String tileString;

for(int r = 0; r < TileVector.size(); ++r)
{
    for(int c = 0; c < TileVector[r].size(); ++c)
    {
        
        if(TileVector[r][c].tileType == "tile")
        {
             ss << "test";
             tileString.SetText(ss.str());
             //tileString.SetText("test"); This works!
             tileString.SetSize(13);
             tileString.SetPosition((c * tileWidth) + 5, (r * tileHeight) + 10);
             window.Draw(tileString);

             // I've even tried clearing ss...
             ss.clear();

        }

    }
}


Anyway, I hope I've provided enough detail. Please let me know if you need any additional information.

Thanks,
Scott




I run into this too when I come back to using stringstream. All of the data you insert into a stringstream doesn't get removed when you call the str() member. You need to clear it out when you want to add new items:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <ssstream>

sf::String tileString;

stringstream ss;

ss << "test";

tileString.SetText( ss.str() );

ss.str( "" );    /*    Clear out ss    */




Perhaps someone has a better idea?
put line 1 right before line 11
Since you keep adding to the stringstream every iteration of your for loops where you encounter a "tile", it's doing exactly what you tell it to. Nothing weird about it.

clear(), as it relates to streams, resets the error states.

Assuming this is simplified code and you actually have a reason to be using a stringstream, try this:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
sstringstream ss ;
sf::String tileString;
for(int r = 0; r < TileVector.size(); ++r)
{
    for(int c = 0; c < TileVector[r].size(); ++c)
    {
        
        if(TileVector[r][c].tileType == "tile")
        {
             ss << "test";
             tileString.SetText(ss.str());
             ss.clear() ;  // reset any error state.
             ss.str("") ;  // reset ss.

             //tileString.SetText("test"); This works!
             tileString.SetSize(13);
             tileString.SetPosition((c * tileWidth) + 5, (r * tileHeight) + 10);
             window.Draw(tileString);
        }
    }
}
That (kooth's) is the correct way to "clear" the stream. I'm not a big fan of this inconsistency with the STL.

The clear function is for clearing the flags:
http://cplusplus.com/reference/iostream/ios/clear/

The str function is for getting or setting the contents of the object:
http://cplusplus.com/reference/iostream/stringstream/str/

Without calling both there is the possibility of one iteration doing something that can affect the other iterations. That is, there are dependencies that are not obvious when using streams.
Thanks, everyone for your help. I suppose it makes sense now knowing that the ss.clear() doesn't actually clear the contents of ss.

I've implemented ss.str("") in the code and it works perfectly!!

Also, apologies for not replying sooner. I wasn't getting email notifications that people were posting on my thread...

Thanks again!!

EDIT: I see the "tracking options for this topic" selection box now...
Last edited on
Glad to help! One last thing: If you could mark this as solved, that would be helpful to others.
Topic archived. No new replies allowed.