In Koenig and Moo's Accelerated C++ book on page 57, they provide the function shown below, which returns in
. The stated reason for doing so is to indicate whether the attempted input was successful (page 55). However, in
was passed by reference as one of the arguments to the function. So can't you just get the state of the istream by looking at the original object?
// read homework grades from an input stream into a `vector<double>'
istream& read_hw(istream& in, vector<double>& hw)
{
if (in) {
// get rid of previous contents
hw.clear();
// read homework grades
double x;
while (in >> x)
hw.push_back(x);
// clear the stream so that input will work for the next student
in.clear();
}
return in;
}