When using:
string s;
cin >> s;
Which characters can string contain and which characters will stop the reading to string.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
std::ctype_base::spaceis the delimiter forstd::istreamwhich makes it stop reading further character from the source.std::ctype_base::spacerefers to whitespace and newline. That means,scan contain any character except whitespace and newline, when reading usingcin>>s.If you want to read complete line containing whitespaces as well, then you can use
getline()function which uses newline as delimiter. There also exists its overloaded function, which you can use if you want to provide your own delimiter. See it’s documentation for further detail.You can also use customized locale which you can set to
std::istream. Your customized locale can define a set of characters to be treated as delimiter bystd::istream. You can see one such example here (see my solution):Right way to split an std::string into a vector<string>