C++ Strings

Started by
2 comments, last by mako_5 18 years, 4 months ago
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
Advertisement
Look up std::stringstream.
    std::ostringstream out;    out << name << " " << hp << " " << mp;    char const * s = out.str().c_str();
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
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";
Awesome,thanks.

This topic is closed to new replies.

Advertisement