list some steps to fix the loop error?

std::cin is going to read until it meats a whitespace.

So when you input “how are you “, cin is going to be called thrice, so that it consumes all three whitespaces. So the first cin will read “how “, the second “are “, and the third “you “.

So what teaches you is that in order to read a sentence, you need another approach, e.g. by using std::getline, like this:

std::getline(std::cin, response)

Read more in What’s the difference between getline and std::istream::operator>>()?


PS: Note the trailing newline character that might appear after reading a number with cin, which you might want to consume, e.g. by using getchar();.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top