Overloading istream
I need to overload the istream to take in rational numbers
Heres what I have,,,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
istream& operator>>( istream& In, Rational& Item )
{
char Ch;
int Num, Den;
In >> Num >> Ch;
if (Ch != '/' or !In.good())
{
In.setstate( ios::failbit );
}
else
{
In >> Den;
}
Item.Numerator_ = Num;
Item.Denominator_ = Den;
return In;
}
|
Here is the driver function that I am trying to run...
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
|
using namespace std;
#include
#include
#include
#include "proj06.rational.h"
/*-----------------------------------------------------------------------------
Name: main
Purpose: Test some of the operations in type Rational
-----------------------------------------------------------------------------*/
int main()
{
Rational A,B,C,D,E;
ifstream Data;
Data.open("proj06.tests", ios::in);
Data >> A >> B >> C >> D >> E;
cout << endl << "----- Testing operator<< -----" << endl;
cout << "Rational A: " << A << endl;
cout << "Rational B: " << B << endl;
cout << "Rational C: " << C << endl;
cout << "Rational D: " << D << endl;
cout << "Rational E: " << E << endl;
|
The proj06.tests is pretty simple it looks like this
1/2
3/4
5/2
1/8
9/12
2/3
4/8
5/9
Topic archived. No new replies allowed.