This is how pointer arithmetics work in C/C++.
Basically, "Hello world!"
is just a const char*
and its +
operator works on it like a pointer. Which means that, for example, if you add 3
to it, then instead of pointing to the beginning of "Hello world"
it will point to 3 characters after the beginning. If you print that, you get llo world
.
So you're not doing a string concatenation there.
If you just want to print the stuff, you can use operator <<
and do something like this instead:
std::cout << "Hello world!" << id << std::endl;
If you need a string that actually works like a string, consider using std::string
from the standard library or if you use something like Qt, you can use QString
.
With a modern compiler (that supports C++11), you can use std::to_string
to create a string from an int
, and then you can do it like this:
std::string s = "Hello world!";
int i;
std::cin >> i;
s += std::to_string(i);
std::cout << s;
If you consider getting seriously into C++ app development, consider using a library like Qt or boost, which make your work just that much easier, although knowing the standard library well is also a good thing. :)