2 ms·
if memory is exhausted, then getline throws a bad_alloc exception. if end of fileis reached (which i am not sure your code deals with correctly) then getline re
by zabzonk 4y ago
if memory is exhausted, then getline throws a bad_alloc exception. if end of fileis reached (which i am not sure your code deals with correctly) then getline returns a stream in a bad state. so something in outline like this (syntactically correct. std:: omitted for brevity, but probably not what you would really write):
string input;
try {
if ( cin.getline ( input ) ) {
// ok read input, do something with it
}
else {
// some sort of predictable error - look at state of cin to diagnose EOF
}
}
catch (...) {
/// something terrible happened - as hard to recover as it would be in c
}