0

Hello. Finishing up the my program that's due soon and I've came across some hiccups you could say.

Basically I am to write a Class definition file and use it to to read in an infinite amount of fraction problems. (+,-,*,/,<,>,<=,>=,==, and !=)

While I have successfully written most of the program I have ran into a problem where my Overloaded <=, !=, and >= are returning the opposite boolean value desired. At first it was different operators that were not working correctly that had to do with comparing but as soon as I switch all my Int value to Float it seemed to correct some but then again broke some others.

I've tried debugging it multiple times and made sure its not my Printing function that isn't messing it up as I am getting the wrong (not desired) boolean value sent to it.

Here is some code:

Class File :

class Fraction
{
    private:
        float num,
              den;
            
    public:
        Fraction(void);
        ~Fraction(void);
        
        void setFract(float top, float bottom);
        void getFract(float & num1, float & den1);
        
        Fraction operator+(const Fraction & fraction) const;
        Fraction operator-(const Fraction & fraction) const;
        Fraction operator/(const Fraction & fraction) const;
        Fraction operator*(const Fraction & fraction) const;
        
        bool operator<(const Fraction & fraction) const;
        bool operator>(const Fraction & fraction) const;
        bool operator<=(const Fraction & fraction) const;
        bool operator>=(const Fraction & fraction) const;
        bool operator==(const Fraction & fraction) const;
        bool operator!=(const Fraction & fraction) const;
};

Implementation file for my Class file (left out the out other functions as they are working correctly):

bool Fraction::operator<(const Fraction & fraction) const
{
    float fractOne,
          fractTwo;
          
    fractOne = num * fraction.den;
    fractTwo = den * fraction.num;
    
    return (fractOne < fractTwo);
}
 
bool Fraction::operator>(const Fraction & fraction) const
{
    float fractOne,
          fractTwo;
          
    fractOne = num * fraction.den;
    fractTwo = den * fraction.num;
    
    return (fractOne > fractTwo);
}
  
bool Fraction::operator<=(const Fraction & fraction) const
{
    float fractOne,
          fractTwo;
          
    fractOne = num * fraction.den;
    fractTwo = den * fraction.num;
    
    return (fractOne <= fractTwo);
}
 
bool Fraction::operator>=(const Fraction & fraction) const
{
    float fractOne,
          fractTwo;
          
    fractOne = num * fraction.den;
    fractTwo = den * fraction.num;
    
    return (fractOne >= fractTwo);
}

bool Fraction::operator==(const Fraction & fraction) const
{
    float fractOne,
          fractTwo;
          
    fractOne = num * fraction.den;
    fractTwo = den * fraction.num;
    
    return (fractOne == fractTwo);
}
     
bool Fraction::operator!=(const Fraction & fraction) const
{
    float fractOne,
          fractTwo;
          
    fractOne = num * fraction.den;
    fractTwo = den * fraction.num;
    
    return (fractOne != fractTwo);

I've tried comparing the fractions in two different ways. First way was to divide

fractOne = num / den;

then compare that to

fractTwo = fraction.num / fraction.den;

but that didn't work. The other way is the way you see above.

The overloaded ==, <, > operators seem to work just fine at the moment.

I don't think it is is necessary to post my "main()" code as it seems to be working perfectly, but if someone feels otherwise I will go ahead and post it up.

but here is my printfunction code:

void PrintFractions(Fraction F1, Fraction F2, Fraction F3, char Oper[2],bool value)
{   
    float Num,
          Den;
    
    F1.getFract(Num, Den);
    outFile << Num << '/' << Den << " ";
    outFile << Oper[0] << " ";
    
    F2.getFract(Num, Den);
    outFile << Num << '/' << Den << " = ";
    
    if(Oper[0] == '<' || Oper[0] == '>')
    {
        if(value)
        {
            outFile << "True"<< endl << endl;
        }
        else
            outFile << "False" << endl << endl;
    }
    else
    {
        F3.getFract(Num, Den);
        outFile << Num << '/' << Den << endl << endl;
    }
    
}

void PrintFractions2(Fraction F1,Fraction F2, char Oper[2],bool value)
{
    float Num,
          Den;
        
    F1.getFract(Num, Den);
    outFile << Num << '/' << Den << " ";
    outFile << Oper[0] << Oper[1] << " ";
    
    F2.getFract(Num, Den);
    outFile << Num << '/' << Den << " = ";
    
    if(value)
    {
        outFile << "True"<< endl << endl;
    }
    else
    {
        outFile << "False" << endl << endl;
    }
}

I have two different ones. One is for the single char operators such as +,*,<,>...ect and the second one if the the two char operators such as ==,!=, <=, and >=.

I'll still be cracking my head over this for the rest of the day but a pair of fresh eyes would be helpful!

Thanks !

Edited by mswezey: n/a

1
Contributor
1
Reply
7
Views
6 Years
Discussion Span
Last Post by mswezey
0

Ha! Wow Yeah I solved it. Might as well delete this thread.

Turns out my Array of char in my main() function in my second of 2 switches was set to Oper[1] instead of Oper[0] !

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.