C++ String Concatenation operator<<

backspace picture backspace · Feb 3, 2014 · Viewed 112.5k times · Source

I have realised my mistake. I was trying to concatenate two strings.

I have just started to learn C++. I have a problem about string concatenation. I don't have problem when I use:

cout << "Your name is"<<name;

But when I try to do it with a string:

string nametext;
nametext = "Your name is" << name;
cout << nametext;

I got an error. How can I concatenate two strings?

Answer

Creris picture Creris · Feb 3, 2014

For string concatenation in C++, you should use the + operator.

nametext = "Your name is" + name;