+ 2

Difference between getline(cin, str ,'\n'); And getline(cin, str);

27th Jul 2017, 3:49 PM
Aditya Lal
Aditya Lal - avatar
6 Antworten
+ 13
getline (cin, str); and getline (cin, str, '\n'); both means the same thing. The third parameter specifies the delimitation character. When this character is met, the rest of the input will be ignored. The default delim for getline function is the newline character '\n'. We actually have pretty legit official documentation! Please refer: http://www.cplusplus.com/reference/string/string/getline/
27th Jul 2017, 4:08 PM
Hatsy Rei
Hatsy Rei - avatar
+ 4
I have not used the first one you listed Does it work?
27th Jul 2017, 4:00 PM
Manual
Manual - avatar
+ 4
I saw not difference in the output. Maybe because the whitespace of the newline was ignored.
27th Jul 2017, 4:09 PM
Manual
Manual - avatar
+ 3
I does not seem work in the playground https://code.sololearn.com/cp32oQ9205NP/?ref=app
27th Jul 2017, 4:05 PM
Manual
Manual - avatar
+ 3
Oh, I did not see @Hatsy's answer. 😸
27th Jul 2017, 4:11 PM
Manual
Manual - avatar
+ 1
I think the first one adds the \n character to the end of the string while the next one just takes string as input and assigns it to str variable
27th Jul 2017, 4:04 PM
Siddharth Saraf