You always need to check the input after reading, i.e., your program should look something like this:
while (std::cin >> name >> age) {
// do something with name and age
}
This will read from std::cin
until something fails. You can check if std::cin.eof()
is set to determine if having reached the end of the of the input is the cause of the error or there was some other failure, e.g., an attempt to enter something which isn't a number for the age
.