Search code examples
c++classstructfractions

How to reduce/simplify fractions completely (C++)


I cannot for the life of me fathom why I'm getting infinite values returned when I input a normal fraction into the code. Everything but the GCD (Greatest common divisor) seems to be working.

Is there a blatantly obvious logic error somewhere within this?

I've done my research and found various answers to the question, I mean Wikipedia even GIVES YOU code to do it, but I'd like to figure out how to make it work the way I've coded it the way it is now.

#include <iostream>
#include <stdlib.h>
#include <cmath>
#include <math.h>

using namespace std;

class Fraction
{
private:
   double num;
   double den;
   double fraction;
   double temp;
public:
   void setNum();
   void setDen();
   int getNum();
   int getDen();
   void lcdOutput();
   void decOutput();
   int gcd();
};

void Fraction::setNum(){
   cout << "Enter a value for your numerator: " << endl;
   cin >> num;
}

void Fraction::setDen(){
    cout << "Enter a value for your denominator: " << endl;
    cin >> den;
}

int Fraction::getNum(){
    return num;
 }

 int Fraction::getDen(){
     return den;
  }

int Fraction::gcd(){

    Fraction set;
    if(num > den){
       if(fmod(num, den) == 0){
          den = temp;
          return temp;
       }
       else{
          den = fmod(num, den);
          set.gcd();
       }
    }
    else{
       if(fmod(den, num) == 0){
          num = temp;
          return temp;
       }
       else{
          num = fmod(den, num);
          set.gcd();
       }
    }
 }

void Fraction::lcdOutput(){
    Fraction set;
    set.gcd();
    num = num / temp;
    den = den / temp;
    cout << "Fraction in lowest terms: " << num << "/" << den << endl;
}

void Fraction::decOutput(){
   double decimal = num / den;
   cout.precision(4);
   cout << "The fraction in decimal form is: " << decimal << endl;
}

int main(){

    Fraction set;

    set.setNum();
    set.setDen();
    set.getNum();
    set.getDen();
    set.lcdOutput();
    set.decOutput();

return 0;
}

Solution

  • Here's what I can determine just by stepping through your code.

    Starting at main, you instantiate an instance of type Fraction named set. You assign its numerator and denominator via calls to set.setNum() and set.setDen(). The calls to getNum() and getDen() do nothing in this case, as they are not being assigned to anything.

    Then you call lcdOutput(), so let us begin stepping through that. You begin by instantiating a LOCAL instance of Fraction (not sure why you want to do this, it appears to me that this may be a conceptual mistake), and then call set.gcd() for that local instance. Calling set.gcd() will call the method for THAT INSTANCE, and it seems to me that what you really want is this->gcd() or simply gcd().

    You follow up by setting num = num / temp and den = den / temp, but temp is still uninitialized at this point. If the variable is left uninitialized, it can (and usually is) pointing to garbage. This probably explains why you are getting nonsensical values returned.