### 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.

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

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182`` ``````#include #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); }``````

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243`` ``````#include #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"<>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.