Overloading Stream extraction operator

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;
}


testdriver.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
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<double>(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:
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;
}
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?
Topic archived. No new replies allowed.