Using stringstream::get() didn't work either. It still failed at both inserting the int value of the enumerator into the stringstream, and outputting it to a string.

07-12-2010

Programmer_P

Should I be creating a new stringstream object each time I need one, instead of trying to use one object for all conversions of int to string? Also, do i need to pass anything to the stringstream's constructor, or is it ok to declare a stringstream like this:

Code:

stringstream currentEnumeratorValue_ss; //for converting the current enumerator value from an int to a string

And then use the << operator right away to insert values into it. Because that's what I'm currently doing.

07-12-2010

pianorain

Quote:

Originally Posted by Programmer_P

Using stringstream::get() didn't work either. It still failed at both inserting the int value of the enumerator into the stringstream, and outputting it to a string.

I'm willing to bet that your stringstream isn't good even before your first integer insertion. You should check.

Quote:

Originally Posted by Programmer_P

Should I be creating a new stringstream object each time I need one, instead of trying to use one object for all conversions of int to string?

I'm willing to bet that your stringstream isn't good even before your first integer insertion. You should check.

You were correct. It wasn't good before the first integer insertion dealing with the map.
An error bit must have been set probably before that in the code, possibly when dealing with the enumerator values vector...

07-12-2010

laserlight

Quote:

Originally Posted by pianorain

s.str(""); //clear the stringstream buffer

If I remember correctly, you may also need to s.clear(); so as to reset any error flags the stringstream might be in. Personally, I think it is simpler to make the stringstream local to such a loop, unless due to performance reasons it turns out that you really need to pull it out of the loop.

07-12-2010

pianorain

Quote:

Originally Posted by laserlight

If I remember correctly, you may also need to s.clear(); so as to reset any error flags the stringstream might be in. Personally, I think it is simpler to make the stringstream local to such a loop, unless due to performance reasons it turns out that you really need to pull it out of the loop.

You are correct; you should s.clear() if you're going to reuse the stringstream. I'd also prefer to make the stringstream local; keep the code simple until it's finished, then optimize where you need it.

stringstream is no longer good. It failed when I attempted to insert an int into it.
We're at element 1 of the vector, which is: 0
stringstream is no longer good. It failed when I attempted to insert an int into it.
We're at element 2 of the vector, which is: 1
stringstream is no longer good. It failed when I attempted to insert an int into it.
We're at element 3 of the vector, which is: 2
stringstream is no longer good. It failed when I attempted to insert an int into it.
We're at element 4 of the vector, which is: 3
stringstream is no longer good. It failed when I attempted to insert an int into it.
We're at element 5 of the vector, which is: 4
stringstream is no longer good. It failed when I attempted to insert an int into it.
We're at element 6 of the vector, which is: 5

EDIT: Ok, so it fails at the second element of the vector of some reason (which is a 0).

07-12-2010

Programmer_P

Does the stringstream not like it when a 0 is inserted into it?
But that doesn't make any sense, since the first element of the vector was a 0 too, and that worked, while the second one failed...
Oh, maybe its because I need to clear the stringstream after using it for each iteration of the loop.

07-12-2010

pianorain

Would you mind humoring me and checking to see if the stringstream is good after you use operator>>? I'm willing to bet it's not.

07-12-2010

Programmer_P

Oh yeah... :D
Perfect. It works now.
Bout' time...
Seriously though, I don't get why a stringstream object wont overwrite any existing content when you use the << operator.

07-12-2010

whiteflags

Why should it magically clear()? You can append stuff to streams. It happens all the time with files... and cout...

07-12-2010

Programmer_P

Quote:

Originally Posted by pianorain

Would you mind humoring me and checking to see if the stringstream is good after you use operator>>? I'm willing to bet it's not.

Yeah, its still good. The problem was apparently the stringstream << operator either does not handle well when you attempt to insert a value that already exists in the stream, and/or it it does not handle inserting into a stream which already has something in it.

Either way, the problem is solved now.

Thanks everyone. :)

07-12-2010

Programmer_P

Quote:

Originally Posted by whiteflags

Why should it magically clear()? You can append stuff to streams. It happens all the time with files... and cout...

I thought cout outputs whatever argument was passed to its parameter, and then clears itself.

I thought would first output the string, clear itself, output the number, clear itself, and then finally output the endl and clear itself. If it appends it, then why does using later cout statements without explicitly clearing the stream only output the new arguments being passed to cout?

07-12-2010

pianorain

Quote:

Originally Posted by Programmer_P

Yeah, its still good. The problem was apparently the stringstream << operator either does not handle well when you attempt to insert a value that already exists in the stream, and/or it it does not handle inserting into a stream which already has something in it.