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 .
my overloaded stream extraction function:
1 2 3 4 5 6 7
istream& operator >> (istream& in, const RationalNumber& rn)
{
char L;
in >> rn.numerator >> L >> rn.denominator;
return in;
}
#include <iostream>
#include "RationalNumber.h"
usingnamespace 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;
elseif(B > A)
cout << B << " is greater than " << A << endl;
if(A <= B)
cout << A << " is less than or equal to " << B << endl;
elseif(B >= A)
cout << B << " is greater than or equal to " << A << endl;
if(A == B)
cout << A << " is equal to " << B << endl;
elseif (A != B)
cout << A << " is not equal to " << B << endl;
}
istream& operator >> (istream& in, const RationalNumber& rn)
{
char L;
in >> rn.numerator >> L >> rn.denominator;
return in;
}
The immediate problem I notice is that your second parameter, rn, is const. This makes no sense - surely you would have gotten a compiler error for trying to read into const objects?