2

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!

4
  • 9
    Your code works for me. The problem is elsewhere. Commented Apr 21, 2010 at 18:55
  • 2
    You need to show more code, since you have a problem and the code you've shown is just fine. What is in tempTotal? Are you sure? What's the condition you're showing the else of? Are loop1 and loop2 what I'd expect them to be? Commented Apr 21, 2010 at 19:04
  • Not to mention, where is store declared, and what is it defined as? Commented Apr 21, 2010 at 19:08
  • 2
    We've all been there with an un-initialised variable :-) Commented Apr 21, 2010 at 20:15

5 Answers 5

4

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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

1 Comment

I've never used boost, but I'll give it a try ^^ Thanks for the tipp!
1

Try the following:

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

string store = toString(tempTotal);

2 Comments

I'm pretty sure I tried that with the same result, but for the sake of it all I'll do it again.
Tried it, returns the value 0.
1

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

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.