Adding to what @DavidHammen said:
The extraction operations leave the trailing '\n'
character in the stream. On the other hand, istream::getline()
discards it. So when you call getline
after an extraction operator, '\n'
is the first character it encounters and it stops reading right there.
Put this after before getline
call extraction:
cin.ignore()
A more robust way of taking input would be something like this:
while (true) {
cout<<"Time:\t";
if (cin>>time) {
cin.ignore(); // discard the trailing '\n'
break;
} else {
// ignore everything or to the first '\n', whichever comes first
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cin.clear(); // clear the error flags
cout << "Invalid input, try again.\n";
}
}