Sign in to follow this  

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

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



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

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