473,397 Members | 2,028 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

C++ Program Fraction

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
7 2791
sicarie
4,677 Expert Mod 4TB
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
1,806 1GB
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
d0ugg
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
d0ugg
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
4,677 Expert Mod 4TB
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
3,652 Expert 2GB
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
d0ugg
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

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

Similar topics

9
by: John Cho | last post by:
// CHO, JOHN #include<iostream> class fracpri{ int whole; int numer; int denom;
6
by: bg_ie | last post by:
Hi, I wish to write a C program which obtains the system time and hence uses this time to print out its ntp equivalent. Am I right in saying that the following is correct for the seconds part...
6
evilmonkey
by: evilmonkey | last post by:
I am very new to programming as well as Java and this is my first post so please forgive me if this is not quite posted correctly. My Problem is that I have only been using scanner to get user input...
1
by: d0ugg | last post by:
Hi, I'm did a fraction program for one of my programming classes and it did compile, however when I'm running the program it crashes for some reason that I do not know. // fraction.cpp ...
2
by: d0ugg | last post by:
Hi, I'm doing a FRACTION program for one of my Programming classes and I'm getting some errors that I can't figure it out. Here is the Assignment: 1. Convert the fraction structure into a...
4
by: d0ugg | last post by:
Hello everyone, I'm creating a program that it is suppose to add, subtract, multiply and also divide fractions and after that, the result has to be reduced to the lowest terms. However, I'm not...
4
by: Semajthewise | last post by:
Hi All For those of you that helped me with the questions on this... Thank you! I believe I have solved all the bugs in the code and wanted to post the final product. This is a set of code to solve...
3
by: Myxamatosis | last post by:
we're given an implementation file to base our class of Fraction off of. Input and output are in the format x/y, with x and y being ints, with the "/" character in the middle. so to create an...
2
by: frozenfirefly | last post by:
okay, so the first class of this program is the Fraction class. and which I supposed to create: 1. method to input numerator & denominator of fraction 2. and a method to reduce the fraction to its...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.