Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Is there any good reason why:

std::string input;
std::getline(std::cin, input);

the getline call won't wait for user input? Is the state of cin messed up somehow?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
420 views
Welcome To Ask or Share your Answers For Others

1 Answer

Most likely you are trying to read a string after reading some other data, say an int.

consider the input:

11
is a prime

if you use the following code:

std::cin>>number;
std::getline(std::cin,input)

the getline will only read the newline after 11 and hence you will get the impression that it's not waiting for user input.

The way to resolve this is to use a dummy getline to consume the new line after the number.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...