2 Replies - 761 Views - Last Post: 13 February 2008 - 08:32 PM Rate Topic: -----

#1 James Bond C++ Spy  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 132
  • Joined: 03-October 07

Getting errors with code

Posted 12 February 2008 - 09:33 PM

I need some help with this code. I am getting errors. The problem is when I ask the user to enter a fraction in the driver file. Look at the the last few lines in the driver file

.cpp
#include <iostream>
using std::istream;
using std::ostream;
using std::cout;
using std::endl;

#include "Rational.h"


RN::RN(int n, int d )
{
	setnum(n);
	setden(d);
}

RN::RN(const RN  )
{
	 num = copy.num;
	 den = copy.den;
		
}

RN::~RN()
{

}

void RN::setden(int d)
{
	if (d > 0)
	{
		den = d;
	}
	else
		if(d < 0)
		{
			d = d * -1;
			num = num * -1;
			den = d;
		}
		else
		{
		cout << "Denominator cannot be 0!"<< endl;
		den = 1;
				
		};
}

void RN::setnum( int n)
{
	num = n;
}

int RN::getden()
{
	return den;
}

int RN::getnum()
{
	return num;
}

RN RN::operator +(const RN & right)
{
	RN add;
	if( den != right.den)
	{ 
		add.num = (num * right.den) + (right.num * den);
		add.den = den * right.den;
	}
	else
	{
		add.num = num + right.num;
		add.den = den;
	};

	
	return add;
}

RN RN::operator -(const RN & right)
{
	RN subtract;
	if( den != right.den)
	{ 
		subtract.num = (num * right.den) - (right.num * den);
		subtract.den = den * right.den;
	}
	else
	{
		subtract.num = num - right.num;
		subtract.den = den;
	};

	return subtract;
}

RN RN::operator *(const RN & right)
{
	RN multiply;
	multiply.den = den * right.den;
	multiply.num = num * right.num;

	return multiply;
}

RN RN::operator /(const RN & right)
{	
	RN divide;
	divide.den = den * right.num;
	divide.num = num * right.den;

	return divide;
}

RN &RN::operator ++(void) //pre increment overloaded
{
	num = num + den;

	return *this;
}


RN RN::operator ++(int) //post increment overloaded
{
	RN temp = *this;
	num = num + den;

	return temp;
}

RN &RN::operator --(void)
{
	num = num - den;

	return *this;
}

RN RN::operator --(int)
{
	RN temp = *this;
	num = num - den;

	return temp;
}

void RN::operator +=(const RN & right)
{
	*this = *this + right;
}


void RN::operator -=(const RN & right)
{
	*this = *this - right;
}

void RN::operator *=(const RN & right)
{
	*this = *this * right;
}

void RN::operator /=(const RN & right)
{
	*this = *this / right;
}

bool RN::operator==(const RN & right)const
   {
	   if((num/den) == (right.num/right.den))
		   return true;
	   else
			return false;
   }

bool RN::operator !=(const RN &right) const
{
	return !(*this == right);
}


bool RN::operator <(const RN &right) const
{
	if( (num / den) < (right.num / right.den ) )
		return true;
	else
		return false;
}

bool RN::operator <=(const RN &right) const
{
	return !(*this > right);
}

bool RN::operator >(const RN &right) const
{
	if( (num / den) > (right.num / right.den ) )
		return true;
	else
		return false;
}

bool RN::operator >=(const RN &right) const
{
	return !(*this < right);
}

ostream & operator<< (ostream & output, const RN & right)
{
	output << right.num << '/' << right.den;
	return output;
}

istream & operator>> (istream & input, RN & right)
{
	input >> right.num;
	input >> right.den;
	return input;
}

RN::operator double() const
{
	return double(num/den);
}

RN::operator float() const
{
	return float(num/den);
}



.h
#ifndef RATIONAL_H
#define RATIONAL_H

#include <iostream>
using std::istream;
using std::ostream;

class RN
{
	friend ostream &operator<<( ostream &, const RN & ); //overload prototype for cout
	friend istream &operator>>( istream &, RN & ); //overload prototype for cin

public:
	RN( int = 1, int = 1); // constructor
	RN( const RN &); // copy constructor
	~RN(); //destructor
	
	int getden(); //get for denominator
	int getnum(); //get for numerator
	void setden( int d ); //set for denominator
	void setnum( int n ); //set for numerator
	
	RN operator+( const RN & ); // overload prototype for +
	RN operator-( const RN & ); // overload prototype for -
	RN operator*( const RN & ); // overload prototype for *
	RN operator/( const RN & ); // overload prototype for /
	
	RN &operator++(); // overload prototype for prefix ++ operator
	RN operator++(int); // overload prototype for postfix ++ operator
	RN &operator--(); //overload prototype for prefix -- operator
	RN operator--(int); //overload prototype for postfix -- operator
	
	void operator+=( const RN &); //overload prototype for += operator
	void operator-=( const RN & ); //overload prototype for -= operator
	void operator/=( const RN & ); //overload prototype for /= operator
	void operator*=( const RN & ); //overload prototype for *= operator
	
	bool operator==( const RN &)const; //overload prototype for == operator
	bool operator!=( const RN &)const; //overload prototype for != operator
	bool operator>( const RN &) const; //overload prototype for > operator
	bool operator<( const RN &) const; //overload prototype for < operator
	bool operator>=(const RN &) const; //overload prototype for >= operator
	bool operator<=(const RN &) const; //overload prototype for <= operator

	operator double() const; //overload prototype for conversion from int to double
	operator float() const; // overload prototype for conversion from int to float
	
private:

	double den; 
	double num;

};

#endif



driver.cpp
#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include "Rational.h"

int main()
{
	RN a(1,2), b(1,4), c(2,3), d(3,4), e(1,5), f(), g(2,-3), h(1,0);

	cout << "Fraction a = " << a << endl << "Fraction b = " << b << endl;
	cout << "Fraction c = " << c << endl << "Fraction d = " << d << endl;
	cout << "Fraction e = " << e << endl << "Fraction f = " << f << endl;
	cout << "Fraction g = " << g << endl << "Fraction h = " << h << endl;
	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 << "a < b = " << ( a < b ) << endl;
	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 << "a != b = " << ( a != b ) << endl;

	
	g += h;
	cout << "g += h = " << g << endl;
	c -= d;
	cout << "c -= d = " << c << endl;
	c *= d;
	cout << "c *= d = " << c << endl;
	a /= b;
	cout << "a /= b = " << a << endl;
	cout << "++e = " << ++e << endl;
	cout << "--e = " << --e << endl;
	cout << "e++ = " << e++ << endl;
	cout << "e-- = " << e-- << endl;
	cout << "Please input new fraction: ";



/*problem here*/
	cin >> f;
	cout << "The f Fraction is: " << f << endl;
	cout << "Fraction f reduced in double form is: " << double(f) << endl;
	cout << "Fraction f reduced in float form is: " << float(f) << endl;


	return 0;
}



Thanks for your help

This post has been edited by James Bond C++ Spy: 12 February 2008 - 10:09 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Getting errors with code

#2 letthecolorsrumble  Icon User is offline

  • Student of The Sun
  • member icon

Reputation: 27
  • View blog
  • Posts: 555
  • Joined: 07-November 07

Re: Getting errors with code

Posted 13 February 2008 - 03:11 AM

Your driver.cpp
#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include "Rational.h"

int main()
{
	RN a(1,2), b(1,4), c(2,3), d(3,4), e(1,5), f(1,5), g(2,-3), h(1,0);

	cout << "Fraction a = " << a << endl << "Fraction b = " << b << endl;
	cout << "Fraction c = " << c << endl << "Fraction d = " << d << endl;
	cout << "Fraction e = " << e << endl << "Fraction f = " << f << endl;
	cout << "Fraction g = " << g << endl << "Fraction h = " << h << endl;
	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 << "a < b = " << ( a < b ) << endl;
	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 << "a != b = " << ( a != b ) << endl;

	
	g += h;
	cout << "g += h = " << g << endl;
	c -= d;
	cout << "c -= d = " << c << endl;
	c *= d;
	cout << "c *= d = " << c << endl;
	a /= b;
	cout << "a /= b = " << a << endl;
	cout << "++e = " << ++e << endl;
	cout << "--e = " << --e << endl;
	cout << "e++ = " << e++ << endl;
	cout << "e-- = " << e-- << endl;
	cout << "Please input new fraction: ";

	int f1=0; //new fraction numerator
	int f2=1; //new fraction denominator

/*problem here*/
		cin >> f1;
	cin >> f2;
	RN nF(f1,f2); //new fraction
	cout << "The f Fraction is: " << nF << endl;
	cout << "Fraction f reduced in double form is: " << double(nF) << endl;
	cout << "Fraction f reduced in float form is: " << float(nF) << endl;


	return 0;
}



More Help? Keep Posting Question!
Was This Post Helpful? 0
  • +
  • -

#3 James Bond C++ Spy  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 132
  • Joined: 03-October 07

Re: Getting errors with code

Posted 13 February 2008 - 08:32 PM

OK I have it working but I don't think it's right. Here are 2 of the requirements of the project.

1) Overload the following operators:
= + - / * << ++ (both pre and post)
+= -= /= *= >> -- (both pre and post)
These operators need to perform their traditional functions.

2) Overload the relational and equality operators: > < != == >= <=
These operators need to perform their traditional functions.

Thanks for any help
.ccp
#include <iostream>
using std::istream;
using std::ostream;
using std::cout;
using std::endl;

#include "Rational.h"


RN::RN(int n, int d )
{
	setnum(n);
	setden(d);
}

RN::RN(const RN &copy )
{
	 num = copy.num;
	 den = copy.den;
		
}

RN::~RN()
{

}

void RN::setden(int d)
{
	if (d > 0)
	{
		den = d;
	}
	else
		if(d < 0)
		{
			d = d * -1;
			num = num * -1;
			den = d;
		}
		else
		{
		cout << "Denominator cannot be 0!"<< endl;
		den = 1;
				
		};
}

void RN::setnum( int n)
{
	num = n;
}

int RN::getden()
{
	return den;
}

int RN::getnum()
{
	return num;
}

RN RN::operator +(const RN & right)
{
	RN add;
	if( den != right.den)
	{
		add.num = (num * right.den) + (right.num * den);
		add.den = den * right.den;
	}
	else
	{
		add.num = num + right.num;
		add.den = den;
	};

	
	return add;
}

RN RN::operator -(const RN & right)
{
	RN subtract;
	if( den != right.den)
	{
		subtract.num = (num * right.den) - (right.num * den);
		subtract.den = den * right.den;
	}
	else
	{
		subtract.num = num - right.num;
		subtract.den = den;
	};

	return subtract;
}

RN RN::operator *(const RN & right)
{
	RN multiply;
	multiply.den = den * right.den;
	multiply.num = num * right.num;

	return multiply;
}

RN RN::operator /(const RN & right)
{	
	RN divide;
	divide.den = den * right.num;
	divide.num = num * right.den;

	return divide;
}

RN &RN::operator ++(void) //pre increment overloaded
{
	num = num + den;

	return *this;
}


RN RN::operator ++(int) //post increment overloaded
{
	RN temp = *this;
	num = num + den;

	return temp;
}

RN &RN::operator --(void)
{
	num = num - den;

	return *this;
}

RN RN::operator --(int)
{
	RN temp = *this;
	num = num - den;

	return temp;
}

void RN::operator +=(const RN & right)
{
	*this = *this + right;
}


void RN::operator -=(const RN & right)
{
	*this = *this - right;
}

void RN::operator *=(const RN & right)
{
	*this = *this * right;
}

void RN::operator /=(const RN & right)
{
	*this = *this / right;
}

bool RN::operator==(const RN & right)const
   {
	   if((num/den) == (right.num/right.den))
		   return true;
	   else
			return false;
   }

bool RN::operator !=(const RN &right) const
{
	return !(*this == right);
}


bool RN::operator <(const RN &right) const
{
	if( (num / den) < (right.num / right.den ) )
		return true;
	else
		return false;
}

bool RN::operator <=(const RN &right) const
{
	return !(*this > right);
}

bool RN::operator >(const RN &right) const
{
	if( (num / den) > (right.num / right.den ) )
		return true;
	else
		return false;
}

bool RN::operator >=(const RN &right) const
{
	return !(*this < right);
}

ostream & operator<< (ostream & output, const RN & right)
{
	output << right.num << '/' << right.den;
	return output;
}

istream & operator>> (istream & input, RN & right)
{
	input >> right.num;
	input >> right.den;
	return input;
}

RN::operator double() const
{
	return double(num/den);
}

RN::operator float() const
{
	return float(num/den);
}



.h

#ifndef RATIONAL_H
#define RATIONAL_H

#include <iostream>
using std::istream;
using std::ostream;

class RN
{
	friend ostream &operator<<( ostream &, const RN & ); //overload prototype for cout
	friend istream &operator>>( istream &, RN & ); //overload prototype for cin

public:
	RN( int = 1, int = 1); // constructor
	RN( const RN &); // copy constructor
	~RN(); //destructor
	
	int getden(); //get for denominator
	int getnum(); //get for numerator
	void setden( int d ); //set for denominator
	void setnum( int n ); //set for numerator
	
	RN operator+( const RN & ); // overload prototype for +
	RN operator-( const RN & ); // overload prototype for -
	RN operator*( const RN & ); // overload prototype for *
	RN operator/( const RN & ); // overload prototype for /
	
	RN &operator++(); // overload prototype for prefix ++ operator
	RN operator++(int); // overload prototype for postfix ++ operator
	RN &operator--(); //overload prototype for prefix -- operator
	RN operator--(int); //overload prototype for postfix -- operator
	
	void operator+=( const RN &); //overload prototype for += operator
	void operator-=( const RN & ); //overload prototype for -= operator
	void operator/=( const RN & ); //overload prototype for /= operator
	void operator*=( const RN & ); //overload prototype for *= operator
	
	bool operator==( const RN &)const; //overload prototype for == operator
	bool operator!=( const RN &)const; //overload prototype for != operator
	bool operator>( const RN &) const; //overload prototype for > operator
	bool operator<( const RN &) const; //overload prototype for < operator
	bool operator>=(const RN &) const; //overload prototype for >= operator
	bool operator<=(const RN &) const; //overload prototype for <= operator

	operator double() const; //overload prototype for conversion from int to double
	operator float() const; // overload prototype for conversion from int to float
	
private:

	double den;
	double num;

};

#endif



driver.cpp
#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include "Rational.h"

int main()
{
	RN a(1,2), b(1,4), c(2,3), d(3,4), e(1,5), f(1,1), g(2,-3), h(1,0);

	cout << "Fraction a = " << a << endl << "Fraction b = " << b << endl;
	cout << "Fraction c = " << c << endl << "Fraction d = " << d << endl;
	cout << "Fraction e = " << e << endl << "Fraction f = " << f << endl;
	cout << "Fraction g = " << g << endl << "Fraction h = " << h << endl;
	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 << "a < b = " << ( a < b ) << endl;
	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 << "a != b = " << ( a != b ) << endl;

	
	g += h;
	cout << "g += h = " << g << endl;
	c -= d;
	cout << "c -= d = " << c << endl;
	c *= d;
	cout << "c *= d = " << c << endl;
	a /= b;
	cout << "a /= b = " << a << endl;
	cout << "++e = " << ++e << endl;
	cout << "--e = " << --e << endl;
	cout << "e++ = " << e++ << endl;
	cout << "e-- = " << e-- << endl;
	cout << "Please input new fraction: ";

	int f1=0; //new fraction numerator
	int f2=1; //new fraction denominator

	cin >> f1;
	cin >> f2;
	RN nF(f1,f2); //new fraction
	cout << "The f Fraction is: " << nF << endl;
	cout << "Fraction f reduced in double form is: " << double(nF) << endl;
	cout << "Fraction f reduced in float form is: " << float(nF) << endl;

	return 0;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1