Jump to content
  • Advertisement
Sign in to follow this  
LittleFreak

Adding strings together

This topic is 5044 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 want to have one string so, char mainstring[128]. And then add 1 charactor strings to each part it. Making one big string. But im not sure how to do this. Any help?

Share this post


Link to post
Share on other sites
Advertisement
if your using C++, use std::string. its purpose is to simplify using strings. you can easily concatanate strings via the + operator. ex:



string first_name = "joe";
string lane_name = "smith";

cout << "Your name is"<< first_name + " " + last_name <<endl;



this will print out "Your name is joe smith". personally i think this makes life a million times easier then using functions. for example, you can make an error logging function which receives a single string as parameters, BUT, you dont have to send it a single string, you can concatnate strings in the function call.

string error = Get_Error();
log(" Error in function X :" + error);

sooo much nicer then using C style methods.

Share this post


Link to post
Share on other sites
thanks man that realy helped i was trying to do like char mainstring[1] = "a";
mainstring[2] = "b";

then expecting it to print ab. Thx for the help.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
The char mainstring[1] = "a"... etc thing you were talking about would work, but don't forget to null terminate your string, otherwise you'll get "ab" followed by garbage. But it's easier to use a class, like graveyard filla suggested, or you can use strcat(), like Palidine suggests. However, his loop isn't entirely correct, because of the null termination thing I mentioned.

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!