Conversions

This is a discussion on Conversions within the C++ Programming forums, part of the General Programming Boards category; I am using a class called Fraction with this format... Code: class Fraction // the Fraction class has two parts ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    22

    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

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    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.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    22

    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

  4. #4
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    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.

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    22

    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String-float conversions
    By Victor4015 in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2005, 02:14 PM
  2. Arithmetic conversions
    By wilksdr in forum C Programming
    Replies: 4
    Last Post: 10-02-2005, 09:10 PM
  3. why are enum type conversions disallowed?
    By krygen in forum C++ Programming
    Replies: 11
    Last Post: 11-24-2004, 10:22 AM
  4. type conversions
    By kocika73 in forum C Programming
    Replies: 2
    Last Post: 10-06-2004, 09:45 AM
  5. Head Banging Floating Point Conversions
    By Davros in forum C++ Programming
    Replies: 9
    Last Post: 02-23-2004, 08:23 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21