Sign in to follow this  

[Resloved] C++ and strings

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this