• Advertisement
Sign in to follow this  

C++ Strings

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

I've found some information on appending to strings in C++ but nothing about how to add another type of data field (i.e. unsigned int) to a string. Is there a conversion method or something? (i'm still migrating over from Java) I'm trying to get a const char* to display text on screen using Irrlicht. The problem is how do I append an integer to a char*. What I'm trying to do is display a character's name followed by their HP/MP int values, but don't know how to dynamically build the display string. The display string would equal : character name + HP + MP

Share this post


Link to post
Share on other sites
Advertisement
Look up std::stringstream.

std::ostringstream out;
out << name << " " << hp << " " << mp;
char const * s = out.str().c_str();

Share this post


Link to post
Share on other sites
EDIT: John wins. ;) That's what I get for sitting here doublechecking in VC++. :P

For std::strings, std::stringstreams are good:


#include <sstream>

std::stringstream s;
s << "You have " << hp << " HP and " << mp << "MP";


Share this post


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

  • Advertisement