In my knowledge when using getline()
after cin
, we need to flush the newline character in the buffer first, before calling getline()
, and we do this by calling cin.ignore()
.
std::string name, address;
std::cin >> name;
std::cin.ignore(); //flush newline character
getline(std::cin,address);
But when using multiple cin
, we do not need to flush the newline character.
std::string firstname, lastname;
std::cin >> firstname;
std::cout << firstname << std::endl;
//no need to flush newline character
std::cin >> lastname;
std::cout << lastname << std::endl;
Why is that? Why is cin.ignore()
necessary in the first case, but not the last?