Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am printing a fraction to a stream with an overloaded << operator. Solution Th

ID: 3527385 • Letter: I

Question

I am printing a fraction to a stream with an overloaded << operator.

Explanation / Answer

The standard approach would be to break your custom input operation up into constituents: std::istream & operator>>(std::istream & in, Fraction & input) // not const! { std::string token; if (!(in >> token)) { return in; } // error int num, den; const bool res = parse_token(token, num, den); // write this! if (!res) { in.setstate(std::ios::failbit); return in; } input.set(num, den); // or whatever } The crux is to write the parse_token(const std::string &, int &, int &) function that determines whether a string represents a valid fraction, and if yes puts the numerator and denominator in the two respective variables. For your operator>> function that you want to overload for the Fraction class, rather than taking an input std::string, parse it, and then attempt to make a new Fraction object from the parsed parameters, you should instead be doing all the parsing of the user's input inside the operator>> overloaded function, since that already has direct access to the input stream. In other words what you're doing is redundant, and a bit confusing ... with the operator>> overload for a Fraction object, the intention should be to take the entire user input and create a Fraction object from that user input ... not go through a couple steps before the Fraction object is complete.