Jump to content
  • Advertisement
Sign in to follow this  
xSKOTTIEx

appending to string?

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

in my main game loop, i have an area where people can input their name. and i have my function (wrapped from dx... isnt the problem) that can get the next typed char. It returns a char, which can be 's' or 'S' if shift is down, basically it takes in input. im storing these names into std::string (namely because i dont want to have to deal with buffer size, etc ). how can i do something like this? char input = getTypedKey(); playerName ..... append? nothing seems to work, i might be missing the obvious, but how would i take this char and add it to the end of the playername string? append gives some errors about const chars.

Share this post


Link to post
Share on other sites
Advertisement

string = string + char;


should work. [edit: though there's probably some quick append operator I forget...]

Share this post


Link to post
Share on other sites
playername.push_back(input);
playername += input;
playername.append(input);


All should work.

Share this post


Link to post
Share on other sites
Of course, all of this assumes you really are using a string, and haven't been misled by false prophets bearing the dread "char *". :) It would help to see your code...

Share this post


Link to post
Share on other sites
Ok, that's all fine.

Another problem I have now.

I want to add a number onto the end of a string, eg
int nTextboxNameNo=5;
string sListTextboxName+=nTextboxNameNo;

How would I do this?

Share this post


Link to post
Share on other sites
Like this:


int nTextboxNameNo=5;
std::stringstream s;
s << nTextboxNameNo;

sListTextboxName += s.str();



or like this with boost:


sListTextboxName += boost::lexical_cast<std::string>(nTextboxNameNo);




If you don't want to use boost, you can of course create a wrapper funciton for the functionality of the first example.

Share this post


Link to post
Share on other sites
Quote:
Original post by utilae
Ok, that's all fine.

Another problem I have now.

I want to add a number onto the end of a string, eg
int nTextboxNameNo=5;
string sListTextboxName+=nTextboxNameNo;

How would I do this?


i think you have commandeered my post.

my getTypedKey returns a BYTE which is just a type def for an 8bit unsigned char. i didnt think to try string = string + char; byt string.push_back() complains that such method does not exist. string += input didnt work when i used it either. and string.append( char ) didnt work because the char isnt const. and if i pass in a pointer (i can get away with that) the variable goes out of scope at the end of the method, or is re-written the next time the user types a key. theres got to be a better way!

thanks for your help guys. rating++ to who helps me tackle this, i have less trivial things to waste my time on... spent too much time on this already.

Share this post


Link to post
Share on other sites
if i take in the char and turn it into a std::string, and append that, it works, but only when youe type very slowly, else it gives random P's and spaces. Anyone know a good solution?

Share this post


Link to post
Share on other sites
Call me old fashioned but I would write a String class with overloaded operators for += that take other strings and floats/ints/chars for appending.

then internally your string class uses good old char array[MaxStringSize] as its string and c-style string functions (sprintf, strcat, strcpy, etc). This gives you complete control over how your all your operations are done. I've found trying to append numbers and whatnot to the end of std::strings kind of "hacky" using string streams.

You can also make your string class use char *, and dynamically allocate and grow your string, this will make it so you aren't limited in size (I have a string class for both types).

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!