Hi,
I wrote a class that can display fractions ex. 1/4 and I cannot figure out how to get >> to process 1/4 and separate them into variables numerator and denominator.

my program just constantly creates RationalNumber Objects when it reaches `cin >> A` .

 ``1234567`` ``````istream& operator >> (istream& in, const RationalNumber& rn) { char L; in >> rn.numerator >> L >> rn.denominator; return in; }``````

testdriver.cpp

 ``12345678910111213141516171819202122232425262728293031323334353637`` ``````#include #include "RationalNumber.h" using namespace std; void main() { RationalNumber A; RationalNumber B; cout << "Enter Fraction 1: "; cin >> A; cout << "Enter Fraction 2: "; cin >> B; cout << A << " + " << B << " = " << A+B << endl; cout << A << " - " << B << " = " << A-B << endl; cout << A << " * " << B << " = " << A*B << endl; cout << A << " / " << B << " = " << A/B << endl; cout << "\n\n"; if(A < B) cout << A << " is less than " << B << endl; else if(B > A) cout << B << " is greater than " << A << endl; if(A <= B) cout << A << " is less than or equal to " << B << endl; else if(B >= A) cout << B << " is greater than or equal to " << A << endl; if(A == B) cout << A << " is equal to " << B << endl; else if (A != B) cout << A << " is not equal to " << B << endl; }``````

RationalNumber.h:

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768`` ``````#include using namespace std; #ifndef RATIONALNUMBER_H #define RATIONALNUMBER_H class RationalNumber { private: int numerator; int denominator; public: //constructor RationalNumber(int = 1, int = 1); //copy constructor RationalNumber(const RationalNumber&); RationalNumber(const RationalNumber*); //destructor ~RationalNumber(); //Predicate Methods bool IsValidNumerator(int); bool IsValidDenominator(int); //Mutators void ReduceF(); double ToDecimal() const { return static_cast(numerator)/denominator; } bool SetNumerator(int); bool SetDenominator(int); //accessors int GetNumerator() const { return numerator; } int GetDenominator() const { return denominator; } //overloading operatorands RationalNumber operator = (const RationalNumber&); RationalNumber operator + (const RationalNumber&); RationalNumber operator - (const RationalNumber&); RationalNumber operator * (const RationalNumber&); RationalNumber operator / (const RationalNumber&); bool RationalNumber::operator < (const RationalNumber&); bool RationalNumber::operator <= (const RationalNumber&); bool RationalNumber::operator > (const RationalNumber&); bool RationalNumber::operator >= (const RationalNumber&); bool RationalNumber::operator == (const RationalNumber&); bool RationalNumber::operator != (const RationalNumber&); friend ostream& operator << (ostream&, const RationalNumber& ); friend istream& operator >> (istream&, const RationalNumber& ); }; ostream& operator << ( ostream&, const RationalNumber& ); istream& operator >> ( istream&, const RationalNumber& ); #endif ``````
Last edited on
InstantDeath wrote:
 ``1234567`` ``````istream& operator >> (istream& in, const RationalNumber& rn) { char L; in >> rn.numerator >> L >> rn.denominator; return in; }``````