Why does std::getline() skip input after a formatted extraction?
问题 I have the following piece of code that prompts the user for their name and state: #include <iostream> #include <string> int main() { std::string name; std::string state; if (std::cin >> name && std::getline(std::cin, state)) { std::cout << "Your name is " << name << " and you live in " << state; } } What I find is that the name has been successfully extracted, but not the state. Here is the input and resulting output: Input: "John" "New Hampshire" Output: "Your name is John and you live in "