# Conversions

• 08-31-2003
Bones
Conversions
I am using a class called Fraction with this format...

Code:

```class  Fraction              // the Fraction class has two parts {         private:                 int denom;                 int numer;         public:                 Fraction(int dm, int nm ) : denom(dm), numer(nm)                 {}                 friend istream& operator >>(istream& s, Fraction& d);                 friend ostream& operator <<(ostream& s, Fraction& d);                 ....... };```
the class is used for a fraction that is entered by a user..
I have to overload comparison operators (<, >, ==, and !=)
and display a message letting the user know what fraction is less than greater than..and so on....

how do i convert the fractions from an int to float???? This is what I have in main....

Code:

```int main() {         cout << "\nEnter a fraction, an operator (+, -, *, /), and  another fraction: "         << "\n      (for example: 1/2 * 3/4)    ";           cin >>fr1;          // overloaded the << & >> operators           cin >> op;           cin >>fr2; . . . .                 if ( fr1 < fr2 )                 {                   cout << fr1;                   cout << "is less than ";                   cout << fr2;                   cout << endl;                 }             if ( fr1 > fr2 )                 { cout << fr1;                   cout << "is greater than ";                   cout << fr2;                   cout << endl;                 }             if ( fr1 == fr2 )                 { cout << fr1;                   cout << "is equal to ";                   cout << fr2;                   cout << endl;                 }             if ( fr1 != fr2 )                 { cout << fr1;                   cout << "is not equal to ";                   cout << fr2;                   cout << endl;                 }```
I'm not sure if I need the following in my class or not???

Code:

``` operator float() const                 {                         float value = numer / denom;                         return value;                 }```
If anyone can help me that would be great....If you need more info please let me know.....thanks
• 08-31-2003
Zach L.
The code you gave, if put in your class will correctly convert your fraction to a float. For the comparison operators, however, I would have them explicitly check the numerator and denominator of the two values though (the integers) without converting, as two very close fractions may come up as equal in that case.
• 08-31-2003
Bones
No idea whats wrong???? Conversion
I have no idea why my program won't work right.....I still can't compare the fractions.

Still won't convert from int to float ..... values end up being 0....
I have attached most of my program code... and I'm tring to get the '<' comparison operator to work....

Code:

``` #include <iostream>      // for cin and cout #include <conio.h>        // for getch #include <math.h>        // for the function to reduce fractions #include <process.h>      // for exit using namespace std; class  Fraction              // the Fraction class has two parts {         private:                 int denom;                 int numer;         public:                 Fraction() : denom(0), numer(1)                 { }                 Fraction(int dm, int nm ) : denom(dm), numer(nm)                 {}                 friend istream& operator >>(istream& s, Fraction& d);                 friend ostream& operator <<(ostream& s, Fraction& d);                 Fraction operator + (Fraction two) const;                 Fraction operator - (Fraction two) const;                 Fraction operator * (Fraction two) const;                 Fraction operator / (Fraction two) const;                 bool operator < (Fraction two) const;                 bool operator > (Fraction two) const;                 bool operator == (Fraction two) const;                 bool operator != (Fraction two) const;                 void lowTerms();                 operator float() const    // conversion operator                 {                         float value = numer / denom;                         return value;                 } }; //*****************Member Functions defined outside the class************ istream& operator >> (istream& s, Fraction& d) {         char slash;         s >> d.numer;         s >> slash;         s >> d.denom;         return s; } ostream& operator << (ostream& s, Fraction& d) {         s << d.numer << '/' << d.denom;         s << endl << endl;         return s; } Fraction Fraction::operator + (Fraction two)const {         int n = numer * two.denom + denom * two.numer;         int d = denom * two.denom;         return Fraction(d,n); } //********************************************************************** Fraction Fraction:: operator - (Fraction two) const {         int n =  numer * two.denom - denom * two.numer;         int d =  denom * two.denom;         return Fraction (d,n); } //********************************************************************** Fraction Fraction:: operator * (Fraction two) const {         int n =  numer * two.numer;         int d =  denom * two.denom;         return Fraction (d,n); } //********************************************************************** Fraction Fraction:: operator / (Fraction two) const {         int n =  numer * two.denom;         int d =  denom * two.numer;         return Fraction (d,n); } //********************************************************************** void Fraction::lowTerms()                    //function to reduce fractions {                                            //to lowest terms - pg 262         int tnum, tden,temp, gcd;         tnum = abs(numer);         tden = abs (denom);         if(tden == 0)         {                 cout <<"Illegal fraction : division by 0"; exit (1);         }         else if (tnum == 0)         {                 numer = 0 ; denom = 1; return;         }         while (tnum !=0)         {                 if (tnum < tden)                 {                         temp = tnum; tnum = tden; tden = temp;                 }                 tnum = tnum -tden;         }         gcd = tden;         numer = numer / gcd;         denom = denom / gcd; } //********************************************************************** bool Fraction:: operator < (Fraction two) const {         float first;         float second;         first = numer / denom;         second = two.numer / two.denom;               return (first < second) ? true : false; } //************************Main Function***************************** void main() {         Fraction fr1, fr2, fr3;      //f1 and f2 are inputs, f3 is the result         float value;         char  op;         char  valid_operator= 'y';         char  another_try='y';         cout << "\n-------- The four-function calculator ---------" << endl;         do         {           cout << "\nEnter a fraction, an operator (+, -, *, /), and another fraction: "                               << "\n      (for example: 1/2 * 3/4)    ";           cin >>fr1;           cin >> op;           cin >>fr2;         switch (op)           {                 case '+':                   fr3 = (fr1 + fr2); //overloading the + operator                   break;                 case '-':                   fr3 = (fr1 - fr2); //overloading the - operator                   break;                 case '*':                     fr3 = (fr1 * fr2);                   break;                 case '/':                   fr3 = (fr1 / fr2);                                     break;                 default:                   valid_operator = 'n';                   break;           }             if (valid_operator == 'y')             {             fr3.lowTerms();             // value = fr1;             //value = fr2;             cout << "\nThe result is "; cout << fr3;             if ( fr1 < fr2 )         {                   cout << fr1;                   cout << "is less than ";                   cout << fr2;                   cout << endl;         }                                         }             else             {             cout << "The operator must be +, -, *, or /. Try again! "  << endl;             valid_operator = 'y';             }           cout << "\tDo another calculation?  ";           cin >> another_try;         } while (another_try == 'y');           getch(); }```

HELP PLZ..............thanks
• 08-31-2003
jlou
In your operator< implementation you are doing integer division and then converting the result to a float, so any fraction that is less than 0 will give a result of 0. Convert the numerator and denominator to a float first separately, then divide those floats.

Zach L is correct that this might cause incorrect results if the values are really close, but I'm not sure how you could implement less than just by doing an integer comparison of the numerator and denominator.
• 09-01-2003
Bones
Lookin' right at it!!!!
Always the smallest thing can seem so great, we just don't see it at all.....

Thanks.....you were right.....my program runs perfectly.....

Cheers