String concatenation in c++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

String concatenation in c++

Why this code outputs "Hi!This!" if user input is "This is string" string s; cin>>s; s="Hi!" + s + "!"; cout<<s;

7th Apr 2018, 10:40 AM
Vladi Petrov
Vladi Petrov - avatar
2 Answers
+ 3
If you replace cin >> s: with getline(cin, s); it also supports full names.
7th Apr 2018, 10:53 AM
Timon Paßlick
+ 2
cin extraction always considers spaces (whitespaces, tabs, new-line...) as terminating the value being extracted, and thus extracting a string means to always extract a single word, not a phrase or an entire sentence. To get an entire line from cin, there exists a function, called getline, that takes the stream (cin) as first argument, and the string variable as second.
7th Apr 2018, 10:59 AM
᠌᠌Brains[Abidemi]
᠌᠌Brains[Abidemi] - avatar