Hi. I'm not especially familiar with c++, but I am required to use it for a class. I wrote this little function:
Code:
int readLines(istream &in,
int (*hand)(string))
{
in.exceptions(istream::failbit | istream::badbit);
while(in.good())
{
string uname;
in >> uname;
hand(uname);
}
return 0;
}
It is supposed to read in lines, and pass the lines to a handler function. I want it to keep reading lines as long as the input stream is in a good state, return 0 if it reaches eof, and throw an exception for any other input stream error.
However, when I tested this, it also throws an exception when eof is reached, which is not what I intended. How do I get it to not throw an exception on eof, but to throw an exception for everything else?
Code:
terminate called after throwing an instance of 'std::ios_base::failure'
what(): basic_ios::clear
Aborted