views:

363

answers:

5

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.

EDIT:

Wow. Seriously. This is kind of embarrassing. I forgot to set my tempTotal variable to something. It was uninitialized, so therefore I couldn't copy anything and the reason the program gave me either a 0 or nothing at all.

Hope people can have a laugh though, so I think this question would now be better suited for deletion since it doesn't really serve a purpose unless xD But thanks to everybody who tried to help me!

+1  A: 

Try the following:

string toString(int tempTotal)
{ 
    ostringstream ss;
    ss << tempTotal;
    return ss.str();
}

string store = toString(tempTotal);
Danvil
I'm pretty sure I tried that with the same result, but for the sake of it all I'll do it again.
SoulBeaver
Tried it, returns the value 0.
SoulBeaver
+4  A: 

Have you tried just outputting the integer as is? If you're only converting it to a string to output it, then don't bother since cout will do that for you.

else
{
    // toString( tempTotal, store ); // Skip this step.
    cout << loop1 << " x " << loop2 << "= " << tempTotal << endl; 
}

I have a feeling that it's likely that tempTotal doesn't have the value you think it has.

Eclipse
+1 - the function works fine as is.
Brian Roach
+1  A: 

If you want to output the integer, you don't even need to convert it; just insert it into the standard output:

int i = 100;
cout << i;

If you want the string representation, you're doing good. Insert it into a stringstream as you did, and ask for it's str().

If that doesn't work, I suggest you minimize the amount of code, and try to pinpoint the actual problem using a debugger :)

xtofl
+2  A: 

I know this doesn't directly answer your question but you don't need to write your own conversion function, you can use boost

#include <boost/lexical_cast.hpp>
using boost::lexical_cast;

//usage example
std::string s = lexical_cast<std::string>(tempTotal);
hamishmcn
I've never used boost, but I'll give it a try ^^ Thanks for the tipp!
SoulBeaver
+1  A: 

Short answer: your method to convert an int to a string works. Got any other questions?

David Thornley