C++ Program Fraction

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • d0ugg
    New Member
    • Jan 2007
    • 41

    C++ Program Fraction

    Hi, Iam writing a code for my class and I cant figure out how to solve it. I will be glad if somebody can teach me how to do something to correct it.
    The program is divided it 3 parts.
    Code:
         switch (symbol) 
         { 
          case '+': 
    		read_frac(fraction* f);
    		print_frac(fraction& a_fraction);
    		answer = add(fraction f1, fraction f2); 
            nome = "sum"; 
            break; 
         case '-': 
    		read_frac(fraction* f);
    		print_frac(fraction& a_fraction);
            answer = sub(fraction f1, fraction f2);
            nome= "difference"; 
            break; 
         case '*': 
    		read_frac(fraction* f);
    		print_frac(fraction& a_fraction);
            answer = mult(fraction f1, fraction f2);
            break; 
         case 'x': 
            answer = mult(fraction f1, fraction f2);
            nome = "product";
            break;
         case '/': read_frac(fraction* f);
    			   print_frac(fraction& a_fraction);
            answer = div(fraction f1, fraction f2);
            nome = "quotient";
            break; 
          } 
          cout << "The " << nome << " of " <<  << " and " << fraction << " is "<< answer << "\n";
    The bold part if where Iam having trouble. I don't know exactly what I have to do to compile


    Thanks ,
    doug
    Last edited by RedSon; Mar 23 '07, 10:08 PM. Reason: No need to bold your code
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    Originally posted by d0ugg
    Hi, Iam writing a code for my class and I cant figure out how to solve it. I will be glad if somebody can teach me how to do something to correct it.
    The program is divided it 3 parts.
    Code:
         switch (symbol) 
         { 
          case '+': 
    		read_frac(fraction* f);
    		print_frac(fraction& a_fraction);
    		answer = add(fraction f1, fraction f2); 
            nome = "sum"; 
            break; 
         case '-': 
    		read_frac(fraction* f);
    		print_frac(fraction& a_fraction);
            answer = sub(fraction f1, fraction f2);
            nome= "difference"; 
            break; 
         case '*': 
    		read_frac(fraction* f);
    		print_frac(fraction& a_fraction);
            answer = mult(fraction f1, fraction f2);
            break; 
         case 'x': 
            answer = mult(fraction f1, fraction f2);
            nome = "product";
            break;
         case '/': read_frac(fraction* f);
    			   print_frac(fraction& a_fraction);
            answer = div(fraction f1, fraction f2);
            nome = "quotient";
            break; 
          } 
          cout << "The " << nome << " of " <<  << " and " << fraction << " is "<< answer << "\n";
    The bold part if where Iam having trouble. I don't know exactly what I have to do to compile


    Thanks ,
    doug
    Doug-

    What part are you having trouble with? Have you tried compiling it? What error message do you get?

    Comment

    • DeMan
      Top Contributor
      • Nov 2006
      • 1799

      #3
      You seem to be passing a type as well as it's instance to most methods
      eg
      instead of
      Code:
      answer = div(fraction f1, fraction f2);
      consider
      Code:
      answer = div(f1, f2);
      This applies to almost every method you call.

      Also, your final print
      Code:
            cout << "The " << nome << " of " <<  << " and " << fraction << " is "<< answer << "\n";
      may not do quite what you want, consider
      Code:
            cout << "The " << nome << " of " <<  f1 << " and " << f2 << " is "<< answer << "\n";

      Comment

      • d0ugg
        New Member
        • Jan 2007
        • 41

        #4
        Okay, thanks for the message. I will try to do that and see if I can compile. Thanks once again,
        Doug

        Comment

        • d0ugg
          New Member
          • Jan 2007
          • 41

          #5
          I did some corrections and still got an error.

          Here is the error that the compile shows:

          c:\documentos\p rogramas\calcul ator\fraction\f raction\calc.cp p(65) : error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'fraction' (or there is no acceptable conversion).

          Here is the code again:



          Code:
          #include <iostream>
          #include <string>
          #include "fraction.h"
          using namespace std;
          	
          fraction mult(fraction f1, fraction f2)
          {
          	fraction f10;
          	int common;
          
          	f10.numerator = (f1.numerator * f2.numerator);
          	f10.denominator = (f1.denominator * f2.denominator);
          
          	common = gcd(f10.numerator, f10.denominator);
          	f10.numerator /= common;
          	f10.denominator /= common;
          
          	return f10;
          }
          
          fraction add(fraction f1, fraction f2)
          {
          	fraction f10;
          	int common;
          	
          	f10.numerator = (f1.numerator + f2.numerator);
          	f10.denominator = (f1.denominator + f2.denominator);
          
          	common = gcd(f10.numerator, f10.denominator);
          	f10.numerator /= common;
          	f10.denominator /= common;
          	
          	return f10;
          }
          
          fraction sub(fraction f1, fraction f2)
          {
          	fraction f10;
          	int common;
          
          	f10.numerator = (f1.numerator - f2.numerator);
          	f10.denominator = (f1.denominator - f2.denominator);
          
          	common = gcd(f10.numerator, f10.denominator);
          	f10.numerator /= common;
          	f10.denominator /= common;
          
          	return f10;
          }
          
          fraction div(fraction f1, fraction f2)
          {
          	fraction f10;
          	int common;
          	
          	f10.numerator = (f1.numerator / f2.numerator);
          	f10.denominator = (f1.denominator / f2.denominator);
          
          	common = gcd(f10.numerator, f10.denominator);
          	f10.numerator /= common;
          	f10.denominator /= common;
          
          	return f10;
          }
          
          
          void read(fraction f)
          {
          	int numerator;
          	int denominator;
          
          	cout << "Enter your numerator: ";
          		cin >> numerator;
          	cout << "Enter your denominator: ";
          	cin >> denominator;
          
          }	
          
          void print(fraction a_fraction)
          {
          
          	cout << a_fraction.numerator << "/" << a_fraction.denominator << endl;
          }
          
          
          int gcd (int u, int v)
          {
          	u = (u < 0) ? -u : u;
          	v = (v < 0) ? -v : v;
          
          while (u > 0)
          {
          	if (u < v)
          {
          	int t = u;
          	u = v;
          	v = t;
          }
          	u -= v;
          }
          return v;
          }
          ----------------------------------
          
          
          
          #include <iostream>
          #include <string>
          #include "fraction.h" 
          using namespace std;
          
          int main() 
          { 
          	fraction f1;
          	fraction f2;
          	fraction f10;
          	fraction f;
          	fraction a_fraction;
          
          	int answer;
          	char symbol = ' ';
          	string nome;
          
             while (true)
             { 
          	cout << "+\tADD\n";
          	cout << "-\tSUBTRACT\n";
          	cout << "* or x\tMULTIPLY\n";
          	cout << "/\tDIVIDE\n";
          	cout << "E\tEXIT\n";
          	cout << "What operation do you want to use?: ";
          		cin >> symbol;
          		if (symbol == 'e' || symbol == 'E')
          			exit(0);
          
               switch (symbol) 
               { 
                case '+': 
          		  read(f);
          		  print(a_fraction);
          		  add(f1, f2);
                  nome = "sum"; 
                  break; 
               case '-': 
          		read(f);
          		print(a_fraction);
          		sub(f1, f2);
                  nome= "difference"; 
                  break; 
               case '*': 
          		read(f);
          		print(a_fraction);
          		mult(f1, f2);
                  break; 
               case 'x': 
                  read(f);
          		print(a_fraction);
          		mult(f1, f2);
                  nome = "product";
                  break;
               case '/': 
          		read(f);
          		print(a_fraction);
          		div(f1, f2);
                  nome = "quotient";
                  break; 
                } 
                cout << "The " << nome << " of " << f1 << " and " << f2 << " is " << answer << "\n"; 
             }
             return 0;
          }
          -----------------------
          
          struct fraction
          {
          	int numerator;
          	int denominator;
          };
          
          fraction mult(fraction f1, fraction f2);
          fraction add(fraction f1, fraction f2);
          fraction sub(fraction f1, fraction f2);
          fraction div(fraction f1, fraction f2);
          void read(fraction f);
          void print(fraction a_fraction);
          int gcd (int u, int v);

          Thank you ,

          Doug

          Comment

          • sicarie
            Recognized Expert Specialist
            • Nov 2006
            • 4677

            #6
            Doug-

            I'm not sure if those programs were all together or in different files, what is on line 65? Can you post 64,65, and 66?

            Comment

            • Ganon11
              Recognized Expert Specialist
              • Oct 2006
              • 3651

              #7
              Is fraction a class you have made? If so, you will have to write your own << code for it - use the function header:

              Code:
              friend ostream& fraction::operator<<(ostream& out, fraction& value);
              // Order of arguments may be switched

              Comment

              • d0ugg
                New Member
                • Jan 2007
                • 41

                #8
                The line of the code that have problem is:


                cout << "The " << nome << " of " << f1 << " and " << f2 << " is " << answer << "\n";
                }

                --

                Error message:

                c:\documentos\p rogramas\calcul ator\fraction\f raction\calc.cp p(65) : error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'fraction' (or there is no acceptable conversion)

                Thanks once again.

                Comment

                Working...