By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 833 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

C++ Program Fraction

P: 41
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.
Expand|Select|Wrap|Line Numbers
  1.      switch (symbol) 
  2.      { 
  3.       case '+': 
  4.         read_frac(fraction* f);
  5.         print_frac(fraction& a_fraction);
  6.         answer = add(fraction f1, fraction f2); 
  7.         nome = "sum"; 
  8.         break; 
  9.      case '-': 
  10.         read_frac(fraction* f);
  11.         print_frac(fraction& a_fraction);
  12.         answer = sub(fraction f1, fraction f2);
  13.         nome= "difference"; 
  14.         break; 
  15.      case '*': 
  16.         read_frac(fraction* f);
  17.         print_frac(fraction& a_fraction);
  18.         answer = mult(fraction f1, fraction f2);
  19.         break; 
  20.      case 'x': 
  21.         answer = mult(fraction f1, fraction f2);
  22.         nome = "product";
  23.         break;
  24.      case '/': read_frac(fraction* f);
  25.                print_frac(fraction& a_fraction);
  26.         answer = div(fraction f1, fraction f2);
  27.         nome = "quotient";
  28.         break; 
  29.       } 
  30.       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
Mar 23 '07 #1
Share this Question
Share on Google+
7 Replies


sicarie
Expert Mod 2.5K+
P: 4,677
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.
Expand|Select|Wrap|Line Numbers
  1.      switch (symbol) 
  2.      { 
  3.       case '+': 
  4.         read_frac(fraction* f);
  5.         print_frac(fraction& a_fraction);
  6.         answer = add(fraction f1, fraction f2); 
  7.         nome = "sum"; 
  8.         break; 
  9.      case '-': 
  10.         read_frac(fraction* f);
  11.         print_frac(fraction& a_fraction);
  12.         answer = sub(fraction f1, fraction f2);
  13.         nome= "difference"; 
  14.         break; 
  15.      case '*': 
  16.         read_frac(fraction* f);
  17.         print_frac(fraction& a_fraction);
  18.         answer = mult(fraction f1, fraction f2);
  19.         break; 
  20.      case 'x': 
  21.         answer = mult(fraction f1, fraction f2);
  22.         nome = "product";
  23.         break;
  24.      case '/': read_frac(fraction* f);
  25.                print_frac(fraction& a_fraction);
  26.         answer = div(fraction f1, fraction f2);
  27.         nome = "quotient";
  28.         break; 
  29.       } 
  30.       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?
Mar 23 '07 #2

DeMan
100+
P: 1,806
You seem to be passing a type as well as it's instance to most methods
eg
instead of
Expand|Select|Wrap|Line Numbers
  1. answer = div(fraction f1, fraction f2);
  2.  
consider
Expand|Select|Wrap|Line Numbers
  1. answer = div(f1, f2);
  2.  
This applies to almost every method you call.

Also, your final print
Expand|Select|Wrap|Line Numbers
  1.       cout << "The " << nome << " of " <<  << " and " << fraction << " is "<< answer << "\n";
  2.  
may not do quite what you want, consider
Expand|Select|Wrap|Line Numbers
  1.       cout << "The " << nome << " of " <<  f1 << " and " << f2 << " is "<< answer << "\n"; 
  2.  
Mar 23 '07 #3

P: 41
Okay, thanks for the message. I will try to do that and see if I can compile. Thanks once again,
Doug
Mar 24 '07 #4

P: 41
I did some corrections and still got an error.

Here is the error that the compile shows:

c:\documentos\programas\calculator\fraction\fracti on\calc.cpp(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:



Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3. #include "fraction.h"
  4. using namespace std;
  5.  
  6. fraction mult(fraction f1, fraction f2)
  7. {
  8.     fraction f10;
  9.     int common;
  10.  
  11.     f10.numerator = (f1.numerator * f2.numerator);
  12.     f10.denominator = (f1.denominator * f2.denominator);
  13.  
  14.     common = gcd(f10.numerator, f10.denominator);
  15.     f10.numerator /= common;
  16.     f10.denominator /= common;
  17.  
  18.     return f10;
  19. }
  20.  
  21. fraction add(fraction f1, fraction f2)
  22. {
  23.     fraction f10;
  24.     int common;
  25.  
  26.     f10.numerator = (f1.numerator + f2.numerator);
  27.     f10.denominator = (f1.denominator + f2.denominator);
  28.  
  29.     common = gcd(f10.numerator, f10.denominator);
  30.     f10.numerator /= common;
  31.     f10.denominator /= common;
  32.  
  33.     return f10;
  34. }
  35.  
  36. fraction sub(fraction f1, fraction f2)
  37. {
  38.     fraction f10;
  39.     int common;
  40.  
  41.     f10.numerator = (f1.numerator - f2.numerator);
  42.     f10.denominator = (f1.denominator - f2.denominator);
  43.  
  44.     common = gcd(f10.numerator, f10.denominator);
  45.     f10.numerator /= common;
  46.     f10.denominator /= common;
  47.  
  48.     return f10;
  49. }
  50.  
  51. fraction div(fraction f1, fraction f2)
  52. {
  53.     fraction f10;
  54.     int common;
  55.  
  56.     f10.numerator = (f1.numerator / f2.numerator);
  57.     f10.denominator = (f1.denominator / f2.denominator);
  58.  
  59.     common = gcd(f10.numerator, f10.denominator);
  60.     f10.numerator /= common;
  61.     f10.denominator /= common;
  62.  
  63.     return f10;
  64. }
  65.  
  66.  
  67. void read(fraction f)
  68. {
  69.     int numerator;
  70.     int denominator;
  71.  
  72.     cout << "Enter your numerator: ";
  73.         cin >> numerator;
  74.     cout << "Enter your denominator: ";
  75.     cin >> denominator;
  76.  
  77. }    
  78.  
  79. void print(fraction a_fraction)
  80. {
  81.  
  82.     cout << a_fraction.numerator << "/" << a_fraction.denominator << endl;
  83. }
  84.  
  85.  
  86. int gcd (int u, int v)
  87. {
  88.     u = (u < 0) ? -u : u;
  89.     v = (v < 0) ? -v : v;
  90.  
  91. while (u > 0)
  92. {
  93.     if (u < v)
  94. {
  95.     int t = u;
  96.     u = v;
  97.     v = t;
  98. }
  99.     u -= v;
  100. }
  101. return v;
  102. }
  103. ----------------------------------
  104.  
  105.  
  106.  
  107. #include <iostream>
  108. #include <string>
  109. #include "fraction.h" 
  110. using namespace std;
  111.  
  112. int main() 
  113.     fraction f1;
  114.     fraction f2;
  115.     fraction f10;
  116.     fraction f;
  117.     fraction a_fraction;
  118.  
  119.     int answer;
  120.     char symbol = ' ';
  121.     string nome;
  122.  
  123.    while (true)
  124.    { 
  125.     cout << "+\tADD\n";
  126.     cout << "-\tSUBTRACT\n";
  127.     cout << "* or x\tMULTIPLY\n";
  128.     cout << "/\tDIVIDE\n";
  129.     cout << "E\tEXIT\n";
  130.     cout << "What operation do you want to use?: ";
  131.         cin >> symbol;
  132.         if (symbol == 'e' || symbol == 'E')
  133.             exit(0);
  134.  
  135.      switch (symbol) 
  136.      { 
  137.       case '+': 
  138.           read(f);
  139.           print(a_fraction);
  140.           add(f1, f2);
  141.         nome = "sum"; 
  142.         break; 
  143.      case '-': 
  144.         read(f);
  145.         print(a_fraction);
  146.         sub(f1, f2);
  147.         nome= "difference"; 
  148.         break; 
  149.      case '*': 
  150.         read(f);
  151.         print(a_fraction);
  152.         mult(f1, f2);
  153.         break; 
  154.      case 'x': 
  155.         read(f);
  156.         print(a_fraction);
  157.         mult(f1, f2);
  158.         nome = "product";
  159.         break;
  160.      case '/': 
  161.         read(f);
  162.         print(a_fraction);
  163.         div(f1, f2);
  164.         nome = "quotient";
  165.         break; 
  166.       } 
  167.       cout << "The " << nome << " of " << f1 << " and " << f2 << " is " << answer << "\n"; 
  168.    }
  169.    return 0;
  170. }
  171. -----------------------
  172.  
  173. struct fraction
  174. {
  175.     int numerator;
  176.     int denominator;
  177. };
  178.  
  179. fraction mult(fraction f1, fraction f2);
  180. fraction add(fraction f1, fraction f2);
  181. fraction sub(fraction f1, fraction f2);
  182. fraction div(fraction f1, fraction f2);
  183. void read(fraction f);
  184. void print(fraction a_fraction);
  185. int gcd (int u, int v);

Thank you ,

Doug
Mar 24 '07 #5

sicarie
Expert Mod 2.5K+
P: 4,677
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?
Mar 25 '07 #6

Ganon11
Expert 2.5K+
P: 3,652
Is fraction a class you have made? If so, you will have to write your own << code for it - use the function header:

Expand|Select|Wrap|Line Numbers
  1. friend ostream& fraction::operator<<(ostream& out, fraction& value);
  2. // Order of arguments may be switched
Mar 25 '07 #7

P: 41
The line of the code that have problem is:


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

--

Error message:

c:\documentos\programas\calculator\fraction\fracti on\calc.cpp(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.
Mar 25 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.