// Time.cpp #include "Time.h" using std::isspace; using std::istream; istream & operator>>(istream & in, Time & t) { // variables for reading the hours and minutes (so we // can leave t unchanged in case of an error) int h = 99; int m = 99; // read hours after skipping whitespace in >> h; if (!in) return in; // no int in stream if (h < 0 || h > 23) { in.setstate(in.failbit); return in; } // read next char and make sure it's a colon char next_char = in.get(); if (!in) return in; if (next_char != ':') { in.putback(next_char); in.setstate(in.failbit); return in; } // make sure next char is not whitespace next_char = in.peek(); if (!in) return in; if (isspace(next_char)) { in.setstate(in.failbit); return in; } // read minutes in >> m; if (!in) return in; if (m < 0 || m > 59) { in.setstate(in.failbit); return in; } // all good (except possibly for the number of digits in // the hours and minutes) t.set(h, m); return in; }