multiple stringstream objects

Hi !
I'm just learning how to use stringstreams. They seem to be powerful but they are confusing too. I want to be able to keep using the same stringstream object to handle conversions of different strings.

I currently have code that looks like this which works with 2 of the stringstreams but when I compile with all 3 stringstream objects i get a First-chance exception - Access violation in the memory and then a big crash. I think I understand the error I'm causing but I don't know how to fix it. Thanks !!!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
stringstream ssStreamOne(strPositionOne);
if( (ssStreamOne >> currentPositionOneInt).fail() )
{
   //TODO: error check here..
}
else
{
   //do some stuff
}

stringstream ssStreamTwo(strPositionTwo);
if( (ssStreamTwo >> currentPositionTwoInt).fail() )
{
   //TODO: error check here..
}
else
{
   //do some stuff
}

stringstream ssStreamThree(strPositionThree);
if( (ssStreamThree >> currentPositionThreeInt).fail() )
{
   //TODO: error check here..
}
else
{
   //do some stuff
}



And I want to change it to something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
stringstream ssStream;

ssStream << strPositionOne;
if( (ssStream>> currentPositionOneInt).fail() )
{
   //TODO: error check here..
}
else
{
   //do some stuff
}

ssStream.str(""); //doenst seem to do anything
ssStream << strPositionTwo;
if( (ssStream >> currentPositionTwoInt).fail() )
{
   //TODO: error check here..
}
else
{
   //do some stuff
}

ssStream.str("");
ssStream << strPositionThree;
if( (ssStream>> currentPositionThreeInt).fail() )
{
   //TODO: error check here..
}
else
{
   //do some stuff
}
1
2
3
4
5
6
7
8
9
10
11
ssStream << strPositionOne;
if( !(ssStream>> currentPositionOneInt).fail() )
{
    //TODO: error check here..

    // the stream is in a failed state at this point
    // to reuse it, the failed state must be cleared first
    
    ssStream.clear() ; // http://cplusplus.com/reference/iostream/ios/clear/

}
Thanks Borges ! Worked like a charm :)
Topic archived. No new replies allowed.