String concatenation in c++ | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 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 Respostas
+ 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