473,545 Members | 2,678 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Fraction program

41 New Member
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 single fraction object passed by value. Keep the
functions in fraction.cpp
3. Create a public constructor
a. taking two integer parameters, the numerator first, followed by the denominator
b. use a default argument to set the denominator to 1 if a denominator is not
specified (remember that it is possible to convert an integer n to the fraction n/1).
c. use the initializer list notation
d. make the constructor inline, defined within the class body.
e. move the “reduce to lowest terms” operation to the end of the constructor
4. Make the print function inline but define the body outside and below the class
declaration using the inline keyword (this syntax will be demonstrated in class).
5. Convert your calc.cpp program to use the fraction class.

--------------------------------------------

Here is my CODE

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

c:\documentos\p rogramas\class fraction\class fraction\class fraction\calc.c pp(70) : fatal error C1075: end of file found before the left brace '{' at 'c:\documentos\ programas\
c:\documentos\p rogramas\class fraction\class fraction\class fraction\fracti on.h(7) : see declaration of 'fraction::mult '
c:\documentos\p rogramas\class fraction\class fraction\class fraction\fracti on.cpp(24) : error C2535: 'fraction fraction::add(f raction,fractio n)' : member function already defined or declared
c:\documentos\p rogramas\class fraction\class fraction\class fraction\fracti on.h(8) : see declaration of 'fraction::add'
c:\documentos\p rogramas\class fraction\class fraction\class fraction\fracti on.cpp(40) : error C2535: 'fraction fraction::sub(f raction,fractio n)' : member function already defined or declared
c:\documentos\p rogramas\class fraction\class fraction\class fraction\fracti on.h(9) : see declaration of 'fraction::sub'
c:\documentos\p rogramas\class fraction\class fraction\class fraction\fracti on.cpp(56) : error C2535: 'fraction fraction::div(f raction,fractio n)' : member function already defined or declared
c:\documentos\p rogramas\class fraction\class fraction\class fraction\fracti on.h(10) : see declaration of 'fraction::div'
c:\documentos\p rogramas\class fraction\class fraction\class fraction\fracti on.cpp(72) : error C2535: 'void fraction::read( fraction *)' : member function already defined or declared
c:\documentos\p rogramas\class fraction\class fraction\class fraction\fracti on.h(11) : see declaration of 'fraction::read '
c:\documentos\p rogramas\class fraction\class fraction\class fraction\fracti on.cpp(82) : error C2535: 'void fraction::print (fraction)' : member function already defined or declared
c:\documentos\p rogramas\class fraction\class fraction\class fraction\fracti on.h(12) : see declaration of 'fraction::prin t'
c:\documentos\p rogramas\class fraction\class fraction\class fraction\fracti on.cpp(85) : fatal error C1075: end of file found before the left brace '{' at 'c:\documentos\ programas\class fraction\class fraction\class fraction\fracti on.h(2)' was matched



Thank you very much

Doug
Apr 23 '07 #1
2 2991
Ganon11
3,652 Recognized Expert Specialist
At least one problem is being caused by a mismatching of braces in your class declaration. Look over that area - I think you have left the constructor open, and you are missing a brace to end the gcd function.
Apr 24 '07 #2
r035198x
13,262 MVP
Read the error messages. They are usually very good hints.

Thread renamed.
Apr 24 '07 #3

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

Similar topics

9
2278
by: John Cho | last post by:
// CHO, JOHN #include<iostream> class fracpri{ int whole; int numer; int denom;
9
2493
by: arun.hallan | last post by:
I need to derive fractions from decimals as so: 0.3333 = 1/3 0.6666 = 2/3 The decimal could also be 0.33, or 0.3333333 but the point is that it that the fraction needs to be extracted. The reason for this is a UI restriction, where users can only enter a percentage. But in the case where a third needs to be expressed, a
0
2276
by: amarok | last post by:
Hello all. I'm a Software Engineering student, and I'm attempting to write a program in Java that does as follows: UML for the class: Fraction() Fraction(numerator: int) Fraction(numerator: int, denominator: int) Fraction(value: String) add(frac: Fraction): Fraction subtract(frac: Fraction): Fraction
6
13395
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...
7
2803
by: d0ugg | last post by:
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. switch (symbol) { case '+': read_frac(fraction* f); print_frac(fraction& a_fraction); answer = add(fraction f1, fraction f2); ...
1
2193
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>
4
14436
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
1
3398
by: jrw133 | last post by:
So i was given this program in class. i am supposed to create a four-function calculator for fractions using a fraction class. Heres what the requirements are:create a member function for each of the four arithmetic operations. For example fadd(), fsub(), fmul() and fdiv(). these member functions iwll each take 1 argument(of type fraction) and...
10
5306
by: Jason | last post by:
I'm making a program that will convert decimal inputs (in this case, in inches) and output a fractional answer. At the moment, I'm only able to output the fractional answer in three parts: A whole number text box, a numerator text box, and a denominator text box. I realize that this is probably the closest I'll get to having fractions...
0
7496
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...
0
7941
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...
1
7452
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...
0
7784
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5354
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5071
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3485
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...
1
1039
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
738
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...

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.