I am having major trouble with a fraction program. i spend hours fixing the errors and it still does not work. this link provides the instructions: http://web.jjay.cuny.edu/~sbakiras/mat272/assignment3.pdf

this is my code:

#ifndef RationalNumbers_H
#define RationaNumbers_H

class RationalNumbers
{
private: 
	double numerator; 
	double denominator;
public: 
	RationalNumbers();
	RationalNumbers operator+(const RationalNumbers &); 
	RationalNumbers operator-(const RationalNumbers &); 
	RationalNumbers operator*(const RationalNumbers &); 
	RationalNumbers operator/(const RationalNumbers &); 
	bool operator<(const RationalNumbers &); 
	bool operator>(const RationalNumbers &); 
	bool operator<=(const RationalNumbers &); 
	bool operator>=(const RationalNumbers &); 
	bool operator==(const RationalNumbers &); 
	bool operator!=(const RationalNumbers &); 
	friend void print (const RationalNumbers &);
	void setFraction(double, double);
};
#endif

#include "header.cpp"
#include <iostream>
using namespace std;

RationalNumbers::RationalNumbers()    
{
	numerator = 0.0;
	denominator = 1.0;
	if (denominator<=0)
	{
		cout<<"The denominator must be greater than zero. Please enter again: "<<endl;
		cin>>denominator;
	}	

	int i; 
	for(i=2; i<=numerator; ++i) 
	{  
		if( ((numerator/i) == ((int)(numerator/i))) && ((denominator/i) == ((int)(denominator/i))) ) 
		{   
			numerator /= i;   
			denominator /= i;   
			--i;	     
		} 
	}
}
RationalNumbers RationalNumbers::operator + (const RationalNumbers &rhs)
{
	RationalNumbers result;
	result.setFraction(numerator * rhs.denominator + rhs.numerator * denominator, denominator * rhs.denominator);
	return result;
}

RationalNumbers RationalNumbers::operator - (const RationalNumbers &rhs)
{
	RationalNumbers result;
	result.setFraction(numerator * rhs.denominator - rhs.numerator * denominator, denominator * rhs.denominator);
	return result;
}

RationalNumbers RationalNumbers::operator * (const RationalNumbers &rhs)
{
	RationalNumbers result;
	result.setFraction(numerator * rhs.numerator, denominator * rhs.denominator);
	return result;
}

RationalNumbers RationalNumbers::operator / (const RationalNumbers &rhs)
{ 
	RationalNumbers result;
	result.setFraction(numerator * rhs.denominator, denominator * rhs.numerator);
	return result;
}

bool RationalNumbers::operator < (const RationalNumbers &rhs)
{
	if ((numerator * rhs.denominator) < (rhs.numerator*denominator)) 
		return true; 
	else 
		return false;
}

bool RationalNumbers::operator > (const RationalNumbers &rhs)
{
	if((numerator * rhs.denominator) > (rhs.numerator *denominator))
		return true;
	else
		return false;
}
bool RationalNumbers::operator <=(const RationalNumbers &rhs)
{
	if(numerator*rhs.denominator <= rhs.numerator*denominator)
		return true;
	else
		return false;
}

bool RationalNumbers::operator >=(const RationalNumbers &rhs)
{
	if(numerator*rhs.denominator >= rhs.numerator*denominator)
		return true;
	else
		return false;
}

bool RationalNumbers::operator ==(const RationalNumbers &rhs)
{
	if(numerator == rhs.numerator && denominator==rhs.denominator)
		return true;
	else
		return false;
}

bool RationalNumbers::operator !=(const RationalNumbers &rhs)
{
	if(numerator == rhs.numerator && denominator==rhs.denominator)
		return false;
	else
		return true;
}

void print (const RationalNumbers &rhs)
{
	cout<<rhs.numerator<<"/"<<rhs.denominator <<endl;
}

void RationalNumbers::setFraction(double num, double den)
{
	numerator = num;
	denominator = den;
}

#include <iostream>
#include "Header.cpp"
using namespace std;

int main()
{
	RationalNumbers a, b;
	cout<<"Enter a fraction for A: ";
	cin>>a;
	cout<<"Enter a fraction for B: ";
	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; 
	if (a < b) 
		cout<<"a < b is true"<<endl;
	else
		cout<<"a < b is false"<<endl;
	if(a > b) 
		cout<<"a > b is true"<<endl; 
	else
		cout<<"a > b is false"<<endl; 
	if(a<=b)
		cout<<"a <= b is true"<<endl;
	else
		cout<<"a <= b is false"<<endl;
	if(a>=b)  
		cout<<"a >= b is true"<<endl;
	else
		cout<<"a >= b is false"<<endl;
	if(a==b) 
		cout<<"a equals b is true"<<endl;
	else
		cout<<"a equals b is false"<<endl;
	if (a!=b) 
		cout<<"a not equal to b is true"<<endl;
	else
		cout<<"a not equal to b is false"<<endl;
	return 0;
}

the errors i get involve the "cout<<" and "cin>>" statements in the main fuction. it says "error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'RationalNumbers' (or there is no acceptable conversion)" Please tell me what i am doing wrong.

Ancient Dragon commented: use code tags -5

oh man..i'm so tired..old me..

line 145, 147: you cannot just directly read user input into RationalNumbers variable (unless you do some operator overloading and I don't think you need to do that). Take it into a standard data type, may be an int or float in your case then call the RationalNumbers constructor with these values, create an object and use that.

line 145, 147: you cannot just directly read user input into RationalNumbers variable (unless you do some operator overloading and I don't think you need to do that). Take it into a standard data type, may be an int or float in your case then call the RationalNumbers constructor with these values, create an object and use that.

my professor said i have to do operation overloading. it is because im coding the overloading in the wrong way?

I doubt if your professor wants you to overload operator '>>'. If not then as I mentioned in my post above, take input into int or float and call the ctor. Otherwise, I don't think you have overloaded this operator as of now and you would have to do that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.