Jump to content
  • Advertisement
Sign in to follow this  

How can I append a number at the end of a string object?

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

So far I've got this u32 attachedControlers = getNumberOfControls(); //vector to hold player names vector<string> names; if (attachedControlers > 1 && multiPlayer(attachedControlers)) for (int i = 0; i < attachedControlers; ++ i) { names.push_back("Player"); } else names.push_back("Player1"); But what I need is something that adds a number to the end based on what i is equal too, so if attachedControlers == 3 I will fill my vector with "Player1", "Player2" and "Player3", but I don't know how to append a number to the end without using c style strings.

Share this post


Link to post
Share on other sites
Advertisement
With std::stringstream:

// #include <sstream> at the top of your file

std::stringstream str;
str << "Player" << attachedControlers;
names.push_back(str.str());

Share this post


Link to post
Share on other sites
You can use std::ostringstream.


std::string Playername = "Player";
std::ostringstream StrStream;
StrStream << i;
Playername.append( StrStream.str() );



also to make it a little more readable, and if you are not afraid of using boost, you could write


std::string Playername = "Player";
Playername += boost::lexical_cast< std::string > ( i );




best Jochen


Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!