Need help with my rational class project

Hey everyone I am working on a project where I have to make a rational class that has a user input 2 fractions and from there the user can add,subtract,multiply or divide the two fractions. For the most part my project works great but the only issue I have is that one a user picks an option such as addition, the two fractions will be added but if the user opts to pick another option such as subtraction the program will then subtract the 2nd fraction from the result of the first option that the user selected. I believe the error is coming from my simplify function but I don't know how to fix it. Hopefully someone can help me and if you can thanks in advance.

The 3 pieces of code I included are my Rational header file, Rational .cpp file and driver .cpp file in that order.

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
//Rational class with function prototypes 
#ifndef RATIONAL_H
#define RATIONAL_H

#include <iostream>
using namespace std;

class Rational
{
public:
	Rational(); //Default Constructor
	Rational(int,int);		//Constructor with paramaters 
	~Rational();			//Destructor 
	void setNumerator(int); //Sets numerator
	void setDenominator(int); //Sets denominator
	void Simplify(int,int);	//Simplifys fraction
	Rational operator+(const Rational &);  //Adds two rational numbers
	Rational operator-(const Rational &);	//Subracts two rational numbers
	Rational operator*(const Rational &);	//Multiplys two rational numbers
	Rational operator/(const Rational &);	//Divides two rational numbers
	void rationalNumber();	//prints fraction in a/b form
	void rationalNumber2(); //prints number in floating point format

private:
	int numerator; //private data member for numerator
	int denominator; //private data member for denominator
};
#endif 


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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
#include "Rational.h"
using namespace std;
Rational::Rational()
{

}
Rational::Rational(int a,int b)
{
	setNumerator(a);
	if(b>0)
		setDenominator(b);
	else
	{
		cout << "The denominator cannot be 0. It will be intialized as 1." << endl;
		denominator=1;
	}
}
Rational::~Rational()
{

}
void Rational::setNumerator(int a)
{
	numerator=a;
}
void Rational::setDenominator(int b)
{
	denominator=b; 
}
void Rational::Simplify(int numerator,int denominator)
{
	int gcd;
	int smallest = numerator > denominator ? numerator : denominator;
	for(int i=1;i<=smallest;i++)
		{
			if(numerator%i==0&&denominator%i==0)
				gcd=i;
		}
	setNumerator(numerator/=gcd);
	setDenominator(denominator/=gcd);
}
void Rational::rationalNumber()
{
	cout << "Rational Number=" << numerator << '/' << denominator << endl;
}
Rational Rational::operator+(const Rational &rhs)
{
	Rational temp(numerator,denominator);
	temp.numerator=(numerator*rhs.denominator)+(rhs.numerator*denominator);
	temp.denominator=denominator*rhs.denominator;
	Simplify(temp.numerator,temp.denominator);
	rationalNumber();
	return Rational(numerator,denominator);
}
Rational Rational::operator-(const Rational &rhs)
{
	Rational temp(numerator,denominator);
	temp.numerator=(numerator*rhs.denominator)-(rhs.numerator*denominator);
	temp.denominator=denominator*rhs.denominator;
	Simplify(temp.numerator,temp.denominator);
	rationalNumber();
	return Rational(numerator,denominator);
}
Rational Rational::operator*(const Rational &rhs)
{
	Rational temp(numerator,denominator);
	temp.numerator=(numerator*rhs.numerator);
	temp.denominator=denominator*rhs.denominator;
	Simplify(temp.numerator,temp.denominator);
	rationalNumber();
	return Rational(numerator,denominator);
}
Rational Rational::operator/(const Rational &rhs)
{
	Rational temp(numerator,denominator);
	temp.numerator=(numerator*rhs.denominator);
	temp.denominator=denominator*rhs.numerator;
	Simplify(temp.numerator,temp.denominator);
	rationalNumber();
	return Rational(numerator,denominator);
}


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
#include <iostream>
#include "Rational.h"

using namespace std;

int main()
{
	int a,b,x,y,choice;
	cout << "Enter the value for the first numerator and denominator" << endl;
	cin >> a >> b;
	Rational fraction1(a,b);
	cout << "Enter the value for the second numerator and denominator" << endl;
	cin >> x >> y;
	Rational fraction2(x,y);

	do
	{
		cout<<"1 . Addition of the two fractions"<<endl;
		cout<<"2 . Subtraction of the two fractions"<<endl;
		cout<<"3 . Multiplication of the two fractions"<<endl;
		cout<<"4 . Division of the two fractions"<<endl;
		cout<<"5 . Exit" << endl;
		cout<<"\nEnter your choice: ";
		cin>>choice;
		switch(choice)
		{
			case 1: fraction1.operator+(fraction2);
					break;
			case 2: fraction1.operator-(fraction2);
					break;
			case 3: fraction1.operator*(fraction2);
					break;
			case 4: fraction1.operator/(fraction2);
					break;
			case 5 :
					break;
			default: cout << "Invalid choice\n";
					break;
		} 
	} while(choice>0&&choice<6);
	system("pause");
	return 0;
}
Last edited on
Line 52: Simplify(temp.numerator,temp.denominator);
Is essentially this->Simplify(/*...*/)
And your Simplify function changes values of this->numerator and this->denominator. So first operand will be changed.
Topic archived. No new replies allowed.