C++ - cin and input with spaces | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

C++ - cin and input with spaces

If I try and input a string, namely "strAny", with cin >> strAny; strAny is truncated when it contains a space. Any way and cin-put a phrase with spaces? I tried enclosing in double and single quotes, but neither.

5th Aug 2019, 6:29 PM
Paolo De Nictolis
Paolo De Nictolis - avatar
2 Answers
+ 5
You can use std::getline( std::cin, strAny ); instead. The method reads input until the newline character (or another delimitation character specified by you) is encountered. More information here: http://www.cplusplus.com/reference/string/string/getline/?kw=getline
5th Aug 2019, 9:41 PM
Shadow
Shadow - avatar
+ 2
So helpful, thank you!
6th Aug 2019, 5:44 PM
Paolo De Nictolis
Paolo De Nictolis - avatar