Step 6 of 6
Challenge: operator>> for your own type
Just like operator<< prints your types, operator>> lets streams read them. If the input is malformed, the convention is to put the stream into a failed state with in.setstate(std::ios::failbit), so while (in >> p) stops and callers can check.
std::istream& operator>>(std::istream& in, Point& p) {
char open, comma, close;
int x, y;
if (in >> open >> x >> comma >> y >> close && open == '(' && comma == ',' && close == ')') {
p = {x, y};
} else {
in.setstate(std::ios::failbit);
}
return in;
}
Notice p is only changed when the whole value was read correctly.
Your turn: write operator>> for Point using the (x,y) format above. The provided main reads points until one fails, then prints the path length (sum of Manhattan distances between consecutive points) and whether the input ended cleanly.