23

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?

Kuba hasn't forgotten Monica
  • 92,715
  • 14
  • 137
  • 288
backspace
  • 299
  • 2
  • 3
  • 7

5 Answers5

35

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

nametext = "Your name is" + name;
Kevin
  • 14,269
  • 7
  • 44
  • 64
Creris
  • 1,118
  • 9
  • 19
  • 3
    Doesn't work, I am getting "Invalid operands to binary expression ('const char *' and 'const char *')" The error lies in this line: std::string helloWorld = "Hello," + " World!"; What am I doing wrong? both "Hello," and " World!" are valid strings (I can declare, assign, and print them seperately), but it's raising some weird error abour char* ? – Gregory Fenn Jan 17 '19 at 15:11
  • 4
    @GregoryFenn C++ is trying to be backwards compatible with C therefore a raw literal string, which is `"string"` will be a const char* and not a C++ string. In c++ you cant overload operators that take primitive types on both sides, which means you have to convert one of them to string. This will work: `std::string helloWorld = "Hello" + std::string(" World!");` – Creris Jan 19 '19 at 00:31
  • Awesome! That makes it a lot clearer :) I'm used to C# so some of these technical points seem quite odd to me, it seems strange to me to try to be backwards compatible, but that's just me. – Gregory Fenn Jan 20 '19 at 10:08
  • @GregoryFenn Yea the raw literal strings in C++ are very confusing if you dont know about them beforehand – Creris Jan 20 '19 at 18:35
13

First of all it is unclear what type name has. If it has the type std::string then instead of

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

you should write

std::string nametext = "Your name is " + name;

where operator + serves to concatenate strings.

If name is a character array then you may not use operator + for two character arrays (the string literal is also a character array), because character arrays in expressions are implicitly converted to pointers by the compiler. In this case you could write

std::string nametext( "Your name is " );
nametext.append( name );

or

std::string nametext( "Your name is " );
nametext += name;
Vlad from Moscow
  • 265,791
  • 20
  • 170
  • 303
4

nametext is an std::string but these do not have the stream insertion operator (<<) like output streams do.

To concatenate strings you can use the append member function (or its equivalent, +=, which works in the exact same way) or the + operator, which creates a new string as a result of concatenating the previous two.

Kotauskas
  • 1,043
  • 10
  • 30
eerorika
  • 223,800
  • 12
  • 181
  • 301
4

You can combine strings using stream string like that:

#include <iostream>
#include <sstream>
using namespace std;
int main()
{
    string name = "Bill";
    stringstream ss;
    ss << "Your name is: " << name;
    string info = ss.str();
    cout << info << endl;
    return 0;
}
Giopet
  • 161
  • 3
  • 5
0

nametext = "Your name is" + name;

I think this should do

Minhaj Arfin
  • 971
  • 8
  • 8