# Issue finding greater fraction

• 12-07-2012
hss1194
Issue finding greater fraction
I had this function working before, until my teacher told me to "make it more flexible", in which I pretty much revamped the project and couldn't fix the function. My problem is that the ints great1 and great2 output their values as rounded to the lower number, which makes the if statement always false (=bool equality). However, the code then reads the bool equality as true, which is not always the correct answer.

Can anyone help? This would help me solve two other functions.

FractionsClass.h:
Code:

```#include<string> using namespace std; #ifndef _fractionsclass_ #define _fractionsclass_ class FractionsClass { public:     //constructor     FractionsClass();     FractionsClass(int);     //destructor     ~FractionsClass();     //functions     void addFrac(); //add two fractions     void subtractFrac(); //subtract two fractions     void multFrac(); //multiply fractions     void divideFrac(); //divide fractions     void reduce(); //reduce fraction     void reducesingle();     bool isGreaterThanFrac(bool,bool); //is one fraction greater than another     bool isLessThanFrac(bool,bool); //is one fraction less than another     bool equalFrac(bool); //check to see if fractions are equivalent     void swap();     //accessors     void setFract(int,int); //set fraction     void changeNum1(int);     void changeDen1(int);     void changeNum2(int);     void changeDen2(int);     void changeNewNum(int);     void changeNewDen(int);     int changeEquality(bool,bool);     int returnNum1();     int returnDen1();     int returnNum2();     int returnDen2();     int returnNewNum();     int returnNewDen(); private:     int num1, den1, num2, den2, n1, d1, n2, d2, newnum, newden, cn1, cd1, cn2, cd2, cnum1, cnum2, mixnum1, holdwhole1, mixnum2, holdwhole2, holdwhole3, newwhole, enter, change, rem1, rem2, newnum2; }; #endif```
FractionsClass.cpp:
Code:

```#include"FractionsClass.h" #include<iostream> #include<conio.h> #include<stdio.h> #include<math.h> using namespace std; FractionsClass::~FractionsClass() {     //getch; } FractionsClass::FractionsClass() {     num1=0;     den1=0;     num2=0;     den2=0; } FractionsClass::FractionsClass(int hold) {     num1=n1;     den1=d1;     num2=n2;     den2=d2; }     ...     bool FractionsClass::isGreaterThanFrac(bool equality, bool equality2)     {         int great1, great2;         great1=returnNum1()/returnDen1();         great2=returnNum2()/returnDen2();         if(great1>great2)         {             equality=true;         }         else         {             equality=false;             if(great1==great2)             {                 equality2=true;             }             else             {                 equality2=false;             }         }         changeEquality(equality,equality2);         return equality;         return equality2;     }     ...     void FractionsClass::changeNum1(int n1)     {         num1=n1;     }     void FractionsClass::changeDen1(int d1)     {         den1=d1;     }     void FractionsClass::changeNum2(int n2)     {         num2=n2;     }     void FractionsClass::changeDen2(int d2)     {         den2=d2;     }     void FractionsClass::changeNewNum(int newnum)     {         newnum=newnum;     }     void FractionsClass::changeNewDen(int newden)     {         newden=newden;     }     int FractionsClass::changeEquality(bool equality, bool equality2)     {         equality=equality;         equality2=equality2;         return equality;         return equality2;     }     int FractionsClass::returnNum1()     {         return num1;     }     int FractionsClass::returnDen1()     {         return den1;     }     int FractionsClass::returnNum2()     {         return num2;     }     int FractionsClass::returnDen2()     {         return den2;     }     int FractionsClass::returnNewNum()     {         return newnum;     }     int FractionsClass::returnNewDen()     {         return newden;     }```
ClassDriver.cpp
Code:

```#include"FractionsClass.h" #include<iostream> #include<conio.h> #include<stdio.h> #include<math.h> using namespace std; int main() {     FractionsClass hold;     bool equality=true, equality2=true;     int a=0, tellme=0, run=0;     int fraction1[2], fraction2[2];     int n1, d1, n2, d2, num1, den1, num2, den2, newnum, newden;     FractionsClass FractionsClass;     while(hold.returnDen1()==0)     {         system("cls");         cout<<"Enter fractions in format ( numerator / denominator ) in improper form"<<endl<<endl;         cout<<"Input first number:  ";         scanf("%i/%i",&fraction1[0],&fraction1[1]);         n1=fraction1[0];         hold.changeNum1(n1);         d1=fraction1[1];         hold.changeDen1(d1);         if(hold.returnDen1()==0)         {             cout<<"  Illegal fraction: divide by 0"<<endl<<"  Press enter to reinput"<<endl;             getch();             continue;         }         else         {             while(hold.returnDen2()==0)             {                 system("cls");                 cout<<"Enter fractions in format ( numerator / denominator ) in improper form"<<endl<<endl;                 cout<<"Input first number:  "<<hold.returnNum1()<<"/"<<hold.returnDen1()<<endl;                 cout<<"Input second number: ";                 scanf("%i/%i",&fraction2[0],&fraction2[1]);                 n2=fraction2[0];                 hold.changeNum2(n2);                 d2=fraction2[1];                 hold.changeDen2(d2);                 if(hold.returnDen2()==0)                 {                     cout<<"  Illegal fraction: divide by 0"<<endl<<"  Press enter to reinput"<<endl;                     getch();                     continue;                 }                 else                 {                     break;                 }             }         }     }     while(a<10000)     {         system("cls");         cout<<"First number:  "<<hold.returnNum1()<<"/"<<hold.returnDen1()<<endl;         cout<<"Second number: "<<hold.returnNum2()<<"/"<<hold.returnDen2()<<endl;         //hold.showFrac();         //hold.showShow();         cout<<endl<<"Choose a function:"<<endl;         cout<<"1. Find a sum of fractions"<<endl;         cout<<"2. Find a difference of fractions  (first-second)"<<endl;         cout<<"3. Find a product of fractions"<<endl;         cout<<"4. Find a quotient of fractions    (first/second)"<<endl;         cout<<"5. Reduce the fractions"<<endl;         //cout<<"6. Make the fractions mixed"<<endl;         //cout<<"7. Make the fractions improper"<<endl;         cout<<"6. Find a greater fraction"<<endl;         cout<<"7. Find a lesser fraction"<<endl;         cout<<"8. Check if fractions are equal to each other"<<endl;         cout<<endl<<"9. Swap the values of the fractions"<<endl;         cout<<"10. Change the current values"<<endl;         //cout<<"13. Change the results view"<<endl;         cout<<endl<<"Enter any other value to exit"<<endl;         cin>>tellme;         system("cls");         ...         else if(tellme==6)         {             //hold.showFrac();             //hold.tempmakeImproper();             hold.isGreaterThanFrac(equality,equality2);             hold.returnNum1();             hold.returnDen1();             hold.returnNum2();             hold.returnDen2();             if(equality=true)             {                 cout<<fraction1[0]<<"/"<<fraction1[1]<<" is greater than "<<fraction2[0]<<"/"<<fraction2[1]<<endl;             }             else if(equality=false)             {                 if(equality2=true)                 {                     cout<<fraction1[0]<<"/"<<fraction1[1]<<" is equal to "<<fraction2[0]<<"/"<<fraction2[1]<<endl;                 }                 else if(equality2=false)                 {                     cout<<fraction2[0]<<"/"<<fraction2[1]<<" is greater than "<<fraction1[0]<<"/"<<fraction1[1]<<endl;                 }             }         }         ...         else         {             return;         }         cout<<endl<<"Would you like to run another function?"<<endl;         cout<<"1. Yes"<<endl;         cout<<"2. No"<<endl;         cin>>run;         if(run==1)         {             a++;             continue;         }         else if(run==2)         {             return;         }     } return 0; }```
• 12-07-2012
Lesshardtofind
Are you required to use integers for great1 and great2?
• 12-07-2012
iMalc
You've kinda missed the point of OOP. A fraction class should contain 2 member variables, not 27!
A class should do one thing and do it well.

An add operation for example, should take two instances of the class and produce a third.

Edit:
You cannot execute two return statments one after another to return two things.
Also, I would use integers for this. To compare fractions, instead of division the numerators by their denominator, multiply the other side by the denominator.
I.e.
Code:

```1  2 - < - <=> 1 * 3 < 2 * 2 2  3```
This way, no floating point is required at all, and you won't get any inaccuracies because of it. Only issue is that you may have to avoid integer overflow on the multiplications.
• 12-08-2012
grumpy
In addition to iMalc's comments, you can also simplify the fractions (e.g. turn 2/4 into the equivalent 1/2). That can be achieved, in general, by dividing both denominator and numerator by their greatest common divisor. For a technique to compute the greatest common divisor of the denominator and numerator, look up Euclid's algorithm. That won't eliminate the possibility of integer overflow that iMalc referred to, but does help reduce chances of it occurring.

Given that a fraction is comparable using standard math operators (equality, greater than, etc) it wouldn't hurt to implement various operators for a Fraction class. That way you can compare two fractions using an expression "if (fract1 > frac2)" rather than being forced to use the less clear "if (frac1.GreaterThan(frac2))".