appending to string?

Started by
7 comments, last by LEET_developer 18 years, 9 months ago
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.
---------Coming soon! Microcosm II.
Advertisement
string = string + char;


should work. [edit: though there's probably some quick append operator I forget...]
playername.push_back(input);playername += input;playername.append(input);


All should work.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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...
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?

HTML5, iOS and Android Game Development using Corona SDK and moai SDK

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.
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.
---------Coming soon! Microcosm II.
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?
---------Coming soon! Microcosm II.
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).

This topic is closed to new replies.

Advertisement