Int parameter for string with stringstream

Well!

I feel really stupid for this question, and I absolutely don't mind if I get taken away for this, but I guess I won't post this unless I at least made a serious attempt to find a solution.

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

As you might have guessed, I'm in the part where I have to work with an integer, which I did. I looked at several sites and saw several standards for converting Int to 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 calling function:

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

      

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

Second, I tried .ToString (), string.valueOf (tempTotal), (string) tempTotal, or just store = temptotal.

Everyone refused to work. When I just tried to do an implicit cast with store = tempTotal it didn't give me any value. When I tried to check the output, it literally didn't print anything. I don't know if something was copied into my string that just isn't a printable character, or if the compiler just ignored it. I really do not know.

So, while I feel like this is indeed a very lame question, I just have to ask:

How can I convert this silly integer to a string using stringstream? Other attempts are more or less irrelevant to me, I just really want to know why my string solution is not working.

EDIT:

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

Hope people can laugh, so I think this question is better suited for deletion now as it doesn't really serve a purpose other than xD. But thanks to everyone who tried to help me!

+2


a source to share


5 answers


Have you tried to just output the integer as is? If you are only converting it to a string for output, then don't worry, as cout does it for you.

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

      



I have a feeling that tempTotal probably doesn't have the meaning that you think it does.

+4


a source


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);

      

+2


a source


Try the following:

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

string store = toString(tempTotal);

      

+1


a source


If you want to output an integer, you don't even need to convert it; just paste it to standard output:

int i = 100;
cout << i;

      

If you need string representation, you are doing well. Insert it into a string stream like you did and ask for it str()

.

If that doesn't work, I suggest you keep the amount of code to a minimum and try to spot the actual problem with a debugger :)

+1


a source


Short answer: your int to string conversion method works. Do you have any other questions?

+1


a source