Sign in to follow this  

Adding strings together

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

http://www.cplusplus.com/ref/cstring/strcat.html

but, why use strings if it's only 1 character long? use a simple char instead and you'll save yourself a lot of headaches. then you can just add them into the mainstring inside a for loop:


for ( int i = 0; i < 128; ++i )
{
char theNewChar = someFunctionThatGetsYourChar();
mainstring[i] = theNewChar;
}



-me

Share this post


Link to post
Share on other sites
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
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

This topic is 4860 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this