This is an operator which is defined in Programming: Principles and Practice Using C++ book. I have two questions. First, why after detecting an invalid format we set a failbit? Second, why after the user input an invalid format for the date and while cin
is in the fail state, the date is constructed by default constructor?
istream& operator>>(istream& is, date& dd)
{
int y, m, d;
char ch1, ch2, ch3, ch4;
is >> ch1 >> d >> ch2 >> m >> ch3 >> y >> ch4;
if (!is) return is;
if (ch1 != '(' || ch2 != ',' || ch3 != ',' || ch4 != ')') { // oops: format error
is.clear(ios_base::failbit);
return is;
}
dd = date{ d,m,y }; // update dd
return is;
}
Aucun commentaire:
Enregistrer un commentaire