• Advertisement
Sign in to follow this  

[Resloved] C++ and strings

This topic is 3133 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey guys, I am in need for some help here. :) I need to put a integer (int) into a std::string. The following code should show what I mean:
int score; //Integer holding the score
std::string scoreText; //string version of the above

score = 430; //Just for a test.

//And here comes the problem. How can I assign the value of score to scoreText? IE
scoreText = score; //This doesn't work


I need the integer value to be converted to a string so I can use it with my bitmap font engine (Just in case anyone is wondering why I am doing this). Any help is greatly appreciated! Thank you in advance! [Edited by - pseudobot on July 23, 2009 4:12:25 AM]

Share this post


Link to post
Share on other sites
Advertisement
You need to use a formating utility like std::stringstream.

std::string scoreText;
std::stringstream convert;
convert << 430;
convert >> scoreText;

Or, the more robust wrapper from boost. boost::lexical_cast
std::string scoreText = boost::lexical_cast<std::string>( 430 );

Share this post


Link to post
Share on other sites
Thank you!
Just one thing: When I try the stringstream methode, I get the following error:
error: aggregate ‘std::stringstream convert’ has incomplete type and cannot be defined|

Am I forgetting to link to a certain library? I am linking against iostream & string at teh moment...

Share this post


Link to post
Share on other sites
You need to #include <sstream>

And also, you can retrieve the string version of convert by doing convert.str() if you don't wish to store it.

Share this post


Link to post
Share on other sites
Thank you both so much!! :)You really helped me out here, never thought I get answers so fast!

Thanks again!! [smile]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement