Overloaded >> Operator i n Rational Numbers class

I'm a fairly new learner of c++, trying to get my head around overloading the >> operator for an assignment on fractions and mixed numbers.

I have a class (Rational) which takes the attributes n_ and d_ (numerator and denominator).

I want to overload the >> operator so that a user can input a number in the form of a mixed number: c,n/d i.e 1,1/2 _or_ just a fraction n/d i.e. 1/2, add them together, and then spit out the answer as a fraction or mixed number.

The code for overloading the >> operator I have below works for mixed numbers (1,1/2 + 1,1/2 returns 3) but if I enter 1/2 + 1/2, 1,1/2 + 1/2 or 1/2 + 1,1/2, it doesn't return anything at all.

It seems like I'm missing something really obvious here, any help will be appreciated. The arithmetic operators and output operators I have working fine, but the input is driving me crazy.

 ``123456789101112131415161718`` ``````std::istream& operator>>(std::istream& in, Rational& r) { int n, d, c; if (in >> c && in.peek() == ',' && in.ignore() && in >> n && in.peek() == '/' && in.ignore() && in >> d) { r = Rational(((c * d) + n), d); std::cout << "IF1" << std::endl; return in; } else if (in >> n && in.peek() == '/' && in.ignore() && in >> d) { r = Rational(n, d); std::cout << "IF2" << std::endl; return in; } std::cout << "R: " << r.n_ << " and " << r.d_ << std::endl; std::cout << "In: " << &in << std::endl; return in; }``````
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125`` ``````// I'm a fairly new learner of c++, trying to get my head around overloading // the >> operator for an assignment on fractions and mixed numbers. // I have a class (Rational) which takes the attributes n_ and d_ // (numerator and denominator). // I want to overload the >> operator so that a user can input a number in the // form of a mixed number: c,n/d i.e 1,1/2 _or_ just a fraction n/d i.e. 1/2, // add them together, and then spit out the answer as a fraction or mixed number. // The code for overloading the >> operator I have below works for mixed // numbers (1,1/2 + 1,1/2 returns 3) but if I enter 1/2 + 1/2, 1,1/2 + 1/2 or // 1/2 + 1,1/2, it doesn't return anything at all. // It seems like I'm missing something really obvious here, any help will be // appreciated. The arithmetic operators and output operators I have working // fine, but the input is driving me crazy. #include #include #include class Rational { public: int n {}, // numerator d {1}; // denominator Rational addRationals(const Rational& other); Rational& operator+=(const Rational& other); friend std::istream& operator>>(std::istream& in, Rational& r); friend Rational operator+(Rational left, const Rational& right) { left += right; return left; } friend std::ostream& operator<<(std::ostream& os, const Rational& other); private: void resolveInput(std::string& expr); void readRational(std::string expr); }; Rational Rational::addRationals(const Rational& other) { n = (n * other.d) + (d * other.n); d *= other.d; return *this; } Rational& Rational::operator+=(const Rational& other) { addRationals(other); return *this; } void waitForEnter(); int main() { Rational a; std::cout << "Please insert a sum of rationales: "; std::cin >> a; waitForEnter(); return 0; } void Rational::resolveInput(std::string& expr) { std::string::size_type pos = expr.find(','); if(std::string::npos == pos ) { // If no comma, then only a/b is possible readRational(expr); return; } readRational(expr.substr(0, pos)); // first chunk until comma expr.erase(0, ++pos); // comma included Rational r1; r1.readRational(expr); *this += r1; } void Rational::readRational(std::string expr) { // expr can be: // a // a/b std::string::size_type pos = expr.find('/'); if(std::string::npos == pos ) { // if no slash, then only numerator n = std::stoi(expr); return; } n = std::stoi(expr.substr(0, pos)); expr.erase(0, ++pos); d = std::stoi(expr); } std::istream& operator>>(std::istream& in, Rational& r) { std::string expr; in >> expr; // beware! Whitespaces break the code // expr can be any of the following: // a/b,c/d (means a/b + c/d) // a,b/c (means a + b/c) // a/b,c (means a/b + c) // a/b // I) read until '+' r.resolveInput(expr); // II) eat the '+' in >> expr; // III) read second expression Rational r1; in >> expr; r1.resolveInput(expr); r += r1; // IV) display the result std::cout << "Result: " << r << '\n'; return in; } std::ostream& operator<<(std::ostream& os, const Rational& other) { return os << other.n << '/' << other.d; } void waitForEnter() { std::cout << "\nPress ENTER to continue...\n"; std::cin.ignore(std::numeric_limits::max(), '\n'); }``````