Parse int to string with stringstream

Posted by SoulBeaver on Stack Overflow See other posts from Stack Overflow or by SoulBeaver
Published on 2010-04-21T18:47:54Z Indexed on 2010/04/21 19:03 UTC
Read the original article Hit count: 381

Filed under:
|
|
|
|

Well!

I feel really stupid for this question, and I wholly don't mind if I get downvoted for this, but I guess I wouldn't be posting this if I had not at least made an earnest attempt at looking for the solution.

I'm currently working on Euler Problem 4, finding the largest palindromic number of two three-digit numbers [100..999].

As you might guess, I'm at the part where I have to work with the integer I made. I looked up a few sites and saw a few standards for converting an Int to a String, one of which included stringstream.

So my code looked like this:

//  tempTotal is my int value I want converted.
void toString( int tempTotal, string &str )
{ 
    ostringstream ss;            // C++ Standard compliant method.
    ss << tempTotal;
    str = ss.str();              // Overwrite referenced value of given string.
}

and the function calling it was:

else
{
    toString( tempTotal, store );
    cout << loop1 << " x " << loop2 << "= " << store << endl; 
}

So far, so good. I can't really see an error in what I've written, but the output gives me the address to something. It stays constant, so I don't really know what the program is doing there.

Secondly, I tried .ToString(), string.valueOf( tempTotal ), (string)tempTotal, or simply store = temptotal.

All refused to work. When I simply tried doing an implicit cast with store = tempTotal, it didn't give me a value at all. When I tried checking output it literally printed nothing. I don't know if anything was copied into my string that simply isn't a printable character, or if the compiler just ignored it. I really don't know.

So even though I feel this is a really, really lame question, I just have to ask:

How do I convert that stupid integer to a string with the stringstream? The other tries are more or less irrelevant for me, I just really want to know why my stringstream solution isn't working.

© Stack Overflow or respective owner

Related posts about c++

Related posts about int