Sign in to follow this  

[C++] strings and short ints...

This topic is 4095 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

For beginners? oh, good. i have a problem with getting the values from a short int type var into a std::string string. i tried the following(+ with static_cast<char>):
Quote:
short ffps= getFPS(); std::string bla; bla.clear(); bla+= ffps;
but to no avail. thanks.

Share this post


Link to post
Share on other sites
In STL there is no coercion support between strings and numbers. Use the std::stringstream:

#include <sstream>
#include <string>

std::stringstream s;
s << getFPS();

// To get the std::string call str() on the std::stringstream
std::cout << s.str();


Greetz,

Illco

Share this post


Link to post
Share on other sites
operator+= for std::string only accepts characters, C-style strings, or std::strings. If you want to convert an integer to a string, you can use std::stringstream:


std::stringstream convert;
short number = 38;

convert << number;

std::string result = convert.str();


Then you can use result. You can also use stringstreams to convert other other way, if you'd like.

Later on in your career, look into boost::lexical_cast, which provides the same functionality but in a cleaner package. You may not quite be ready for Boost, yet. It can be daunting...

Share this post


Link to post
Share on other sites

This topic is 4095 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.

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