473,770 Members | 1,779 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C++ Program Fraction

41 New Member
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 2815
sicarie
4,677 Recognized Expert Moderator Specialist
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 Top Contributor
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 New Member
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 New Member
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:



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 Recognized Expert Moderator Specialist
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 Recognized Expert Specialist
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 New Member
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.
Mar 25 '07 #8

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

Similar topics

9
2304
by: John Cho | last post by:
// CHO, JOHN #include<iostream> class fracpri{ int whole; int numer; int denom;
6
19986
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 of the ntp time? long int ntp.seconds = time(NULL) + 2208988800;
6
13431
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 into most of the exercises I have done. This exercise is asking for a user to enter two fractionslike "1/3" or "5/8". Scanner doesn't work and I don't know of another way to get this done. I think that I will have to somehow strip the "/" out and...
1
2214
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 #include <iostream> #include <string>
2
3021
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 class named fraction declared in a file named fraction.h, with two private data members: an int numerator and an int denominator. 2. Convert the four arithmetic functions named add, sub, mult, and div into public member functions, each accepting a...
4
14452
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 sure how the algorithm of reducing fractions works. Here is part of my code: //Header File
4
1875
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 basic frational arithmatic. using only 3 textboxes. Allowing the user to enter any type of fraction and get a proper reduced fraction answer. Feel Free to use it if you like it. Had to do 2 posts sorry was too long I guess. Parameters: VB.Net code...
3
3101
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 object of class Fraction, it'd need to be like Fraction frac1 (3/4) however I'm not sure on how to create this fraction with the format of the .h file we're given class Fraction
2
5974
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 smallest stage. example: input: 6/8 reduce to: 3/4 (both are devided by the gcd which is 2) I think the problem is around the reduce method... about the gcd. please tell me what's wrong.
0
9617
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10099
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10037
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8931
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2849
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.