Avatar of snyderchip123
snyderchip123
 asked on

Implement class into main menu add multiply divide and subtract functions.

I am having trouble overloading my functions and implementating them into my main menu.  


I need to write a program that lets the user perform arithmetic operations on fractions. Fractions are of the form a/b, where a and b are integers and b is not equal to 0. Your program must be menu driven, allowing the user to select the operation (+, -, *, /) and input the numerator and denominator of each fraction. Your program must consist of at least the following functions:

a. Function menu: This function informs the user about the program's purpose, explains how to enter data, and allows the user to select the operation. (5 points)

b. Function addFractions: This function adds two fractions together (or you can overload the +operator()). (5 points)

c. Function subtractFractions: This function subtracts one fraction from another (or you can overload the -operator()). (5 points)

d. Function multiplyFractions: This function multiplies two fractions together (or you can overload the *operator()). (5 points)

e. Function divideFractions: This function divides one fraction into another (or you can overload the /operator()). (5 points)

Some sample outputs are:
3 / 4 + 2 / 5 = 23 / 20
2 / 3 * 3 / 5 = 6 / 15

Your answers need not be in the lowest terms.
Be sure that your program handles exceptions such as division by zeroand non-integer values . Allow the user to immediately re-enter any values which have thrown an exception Allow the user to continue to use the program until they quit via a menu option.  

Here is my program:
#ifndef FRACTION_H
#define FRACTION_H
#include <iostream>
#include <cmath>
using namespace std;
 
class Fraction {
public:                             
    Fraction();                     
    Fraction(int);                    
    Fraction(int, int);              
    void Read();
    void Write() const;              
    Fraction Add(Fraction) const;     
    Fraction Subtract(Fraction) const;
    Fraction Multiply(Fraction) const;
    Fraction Divide(Fraction) const;  
private:                              
    int num;
    int denom;
 
Fraction::Fraction() 
{ 
    num = 0;
    denom = 1;
}
 
Fraction::Fraction(int n) { 
    num = n;
    denom = 1;
}
 
Fraction::Fraction(int n, int d) { 
    num = n;
    denom = d;
}
 
void Fraction::Read() {
    char slash;
    cout << "Enter fraction(numerator / denominator): ";
    cin >> num >> slash >> denom;
    while(slash != '/') {
        cout << "\nERROR! Work with fractions and use numerator / denominator! REENTER!\n";
        cin >> num >> slash >> denom;
    }
}
 
void Fraction::Write() const{ 
    cout << num << " / " << denom ;
}
 
 
 
Fraction Fraction::Add(Fraction someFraction) const { 
    Fraction result;
    result.num = num * someFraction.denom + denom * someFraction.num;
    result.denom = denom * someFraction.denom;
    return result;
}
 
Fraction Fraction::Multiply(Fraction someFraction) const { 
    Fraction result;
    result.num = num * someFraction.num;
    result.denom = denom * someFraction.denom;
    return result;
}
 
Fraction Fraction::Subtract(Fraction someFraction) const { 
    Fraction result;
    result.num = num * someFraction.denom - denom * someFraction.num;
    result.denom = denom * someFraction.denom;
    return result;
}
 
Fraction Fraction::Divide(Fraction someFraction) const { 
    Fraction result;
    result.num = num * someFraction.denom;
    result.denom = denom * someFraction.num;
    return result;
 
};
 
} 
#endif
 
main
 
#include <iostream>
#include <cmath>
#include <iomanip>
#include "Fraction.h"
 
using namespace std;
 
int main()
{
    
    int n1=0;
    int d1=0;
    int n2=0;
    int d2=0;
    int na=0;
    int da=0;
    int num;
  
      
   cout<<"This program performs operations on fractions"<<endl;
   menu(num,n1,n2,d1,d2);
 
   while(num !=9)
   {
             if (num = 1)
             {
                Add(Fraction);
                     }
              if (num =2)
              {
                Subtract(Fraction);
                      }
               if (num =3)
               {
                 Multiply(Fraction);
                       }
               if (num =4)
               {
                  Divide(Fraction);
						}
      }
     
     
    
    system("PAUSE");
    return EXIT_SUCCESS;
}
 
    void menu(int,int,int,int,int)
    {
      int num;   
      int n1;
      int n2;
      int d1;
      int d2;
     
    
      
  cout<<"Enter:"<<endl;
   cout<<"1 :To add a fraction"<<endl;
   cout<<"2 :To subtract a fraction"<<endl;
   cout<<"3 :To multiply a fraction"<<endl;
   cout<<"4 :To divide a fraction"<<endl;
   cout<<"9 :To exit the program"<<endl;
   cin>>num;
   cout<<"For fraction 1"<<endl;
   cout<<"Enter the numerator"<<endl;
   cin>>n1;   
   cout<<"Enter the denominator"<<endl;
   cin>>d1;
   
   cout<<"For fraction 2"<<endl;
   cout<<"Enter the numerator"<<endl;
   cin>>n2;
   
   cout<<"Enter the denominator"<<endl;
   cin>>d2;
	}

Open in new window

C++Programming Languages-Other

Avatar of undefined
Last Comment
snyderchip123

8/22/2022 - Mon
jkr

There are a couple of issues - first, don't write the implementation of your methods into your class' body. your 'menu()' requires a decklaration and your operation of the 'Fraction' class simply miss an object. See the corrections and annotations in the following:
#ifndef FRACTION_H
#define FRACTION_H
#include <iostream>
#include <cmath>
//using namespace std; better not here
 
class Fraction {
public:                             
    Fraction();                     
    Fraction(int);                    
    Fraction(int, int);              
    void Read();
    void Write() const;              
    Fraction Add(Fraction) const;     
    Fraction Subtract(Fraction) const;
    Fraction Multiply(Fraction) const;
    Fraction Divide(Fraction) const;  
private:                              
    int num;
    int denom;
}; // <--- missing 
Fraction::Fraction() 
{ 
    num = 0;
    denom = 1;
}
 
 
 
using namespace std;
 
Fraction::Fraction(int n) { 
    num = n;
    denom = 1;
}
 
Fraction::Fraction(int n, int d) { 
    num = n;
    denom = d;
}
 
void Fraction::Read() {
    char slash;
    cout << "Enter fraction(numerator / denominator): ";
    cin >> num >> slash >> denom;
    while(slash != '/') {
        cout << "\nERROR! Work with fractions and use numerator / denominator! REENTER!\n";
        cin >> num >> slash >> denom;
    }
}
 
void Fraction::Write() const{ 
    cout << num << " / " << denom ;
}
 
 
 
Fraction Fraction::Add(Fraction someFraction) const { 
    Fraction result;
    result.num = num * someFraction.denom + denom * someFraction.num;
    result.denom = denom * someFraction.denom;
    return result;
}
 
Fraction Fraction::Multiply(Fraction someFraction) const { 
    Fraction result;
    result.num = num * someFraction.num;
    result.denom = denom * someFraction.denom;
    return result;
}
 
Fraction Fraction::Subtract(Fraction someFraction) const { 
    Fraction result;
    result.num = num * someFraction.denom - denom * someFraction.num;
    result.denom = denom * someFraction.denom;
    return result;
}
 
Fraction Fraction::Divide(Fraction someFraction) const { 
    Fraction result;
    result.num = num * someFraction.denom;
    result.denom = denom * someFraction.num;
    return result;
 
};
 
//} <--- not needed
#endif
 
 
 
#include <iostream>
#include <cmath>
#include <iomanip>
#include "Fraction.h"
 
using namespace std;
 
 void menu(int,int,int,int,int); // declaration needed
 
int main()
{
    
    int n1=0;
    int d1=0;
    int n2=0;
    int d2=0;
    int na=0;
    int da=0;
    int num;
  
      
   cout<<"This program performs operations on fractions"<<endl;
   menu(num,n1,n2,d1,d2); 
 
   while(num !=9)
   {
             if (num = 1)
             {
                Add(Fraction); // where's the 'Fraction' object? What's the argument?
                     }
              if (num =2)
              {
                Subtract(Fraction);  // where's the 'Fraction' object? What's the argument?
                      }
               if (num =3)
               {
                 Multiply(Fraction);  // where's the 'Fraction' object? What's the argument?
                       }
               if (num =4)
               {
                  Divide(Fraction); // where's the 'Fraction' object? What's the argument?
                                                }
      }
     
     
    
    system("PAUSE");
    return EXIT_SUCCESS;
}
 
    void menu(int,int,int,int,int)
    {
      int num;   
      int n1;
      int n2;
      int d1;
      int d2;
     
    
      
  cout<<"Enter:"<<endl;
   cout<<"1 :To add a fraction"<<endl;
   cout<<"2 :To subtract a fraction"<<endl;
   cout<<"3 :To multiply a fraction"<<endl;
   cout<<"4 :To divide a fraction"<<endl;
   cout<<"9 :To exit the program"<<endl;
   cin>>num;
   cout<<"For fraction 1"<<endl;
   cout<<"Enter the numerator"<<endl;
   cin>>n1;   
   cout<<"Enter the denominator"<<endl;
   cin>>d1;
   
   cout<<"For fraction 2"<<endl;
   cout<<"Enter the numerator"<<endl;
   cin>>n2;
   
   cout<<"Enter the denominator"<<endl;
   cin>>d2;
        }

Open in new window

snyderchip123

ASKER
Ok so when you say the menu is missing a declaration do you mean something of this nature:


    void menu(int num,int n1,int n2,int d1,int d2)

I am still confused on how to output in the class.

I am getting error message:

Error      4      error C2275: 'Fraction' : illegal use of this type as an expression      
Error      3      error C3861: 'Add': identifier not found      

Can you explain what I am doing wrong to call my header.  Are my member functions setup correctly?




jkr

>>error C2275: 'Fraction' : illegal use of this type as an expression  

That's all these lines where I added "where's the 'Fraction' object? What's the argument?"

That should be something like
int main()
{
    
    int n1=0;
    int d1=0;
    int n2=0;
    int d2=0;
    int na=0;
    int da=0;
    int num;
  
      
   cout<<"This program performs operations on fractions"<<endl;
   menu(num,n1,n2,d1,d2); 
 
   Fraction frac1(n1,d1);
   Fraction frac2(n2,d2);
 
   while(num !=9)
   {
             if (num = 1)
             {
                frac1.Add(frac2); 
                     }
              if (num =2)
              {
                frac1.Subtract(frac2);  
                      }
               if (num =3)
               {
                 frac1.Multiply(frac2);  
                       }
               if (num =4)
               {
                  frac1.Divide(frac2); 
                                                }
      }
     
     
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

Open in new window

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
snyderchip123

ASKER
Ok now I am getting the hang of this.  So this code means
frac1.Add(frac2) to add  fraction one to fraction two.  
but does this call my operator?  I think it is by calling fraction two in () correct?

Fraction Fraction::Add(Fraction someFraction) const {
    Fraction result;
    result.num = num * someFraction.denom + denom * someFraction.num;
    result.denom = denom * someFraction.denom;
    return result;
}

I have two erros left about my menu.  

Error      2      error C2365: 'menu' : redefinition; previous definition was 'formerly unknown identifier'      
Error      1      error C3861: 'menu': identifier not found      

Am I not calling the void menu statement correctly.

  menu(num,n1,n2,d1,d2);
void menu(int num,int n1,int n2,int d1,int d2)
    {
         
jkr

>>but does this call my operator?

Yes, 'frac1' will be adding 'frac2'. Regarding the last point, pass the integers to 'menu()' as references, since you want to get their values in 'main()'. The following compiles for me:
#include <iostream>
#include <cmath>
#include <iomanip>
#include "Fraction.h"
 
using namespace std;
 
 void menu(int&,int&,int&,int&,int&); // declaration needed
 
int main()
{
    
    int n1=0;
    int d1=0;
    int n2=0;
    int d2=0;
    int na=0;
    int da=0;
    int num;
  
      
   cout<<"This program performs operations on fractions"<<endl;
   menu(num,n1,n2,d1,d2); 
 
   Fraction frac1(n1,d1);
   Fraction frac2(n2,d2);
 
   while(num !=9)
   {
             if (num = 1)
             {
                frac1.Add(frac2); 
                     }
              if (num =2)
              {
                frac1.Subtract(frac2);  
                      }
               if (num =3)
               {
                 frac1.Multiply(frac2);  
                       }
               if (num =4)
               {
                  frac1.Divide(frac2); 
                                                }
      }
     
     
    
    system("PAUSE");
    return EXIT_SUCCESS;
}
 
    void menu(int& num,int& d1,int& d2,int& n1,int& n2)
    {
 
     
    
      
  cout<<"Enter:"<<endl;
   cout<<"1 :To add a fraction"<<endl;
   cout<<"2 :To subtract a fraction"<<endl;
   cout<<"3 :To multiply a fraction"<<endl;
   cout<<"4 :To divide a fraction"<<endl;
   cout<<"9 :To exit the program"<<endl;
   cin>>num;
   cout<<"For fraction 1"<<endl;
   cout<<"Enter the numerator"<<endl;
   cin>>n1;   
   cout<<"Enter the denominator"<<endl;
   cin>>d1;
   
   cout<<"For fraction 2"<<endl;
   cout<<"Enter the numerator"<<endl;
   cin>>n2;
   
   cout<<"Enter the denominator"<<endl;
   cin>>d2;
        }

Open in new window

snyderchip123

ASKER
Great thanks for explaining all of this...

I am not sure what is wrong there are no errors but it won't actually perform the math.  
I tried to add  a fraction and i just let me enter the num  the denominator for both fractions and then just stops.  Do you know why this would be happening?

I
âš¡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
jkr

Little correctio - there's a problem with 'if (num = x)'. '=' assigns a value, you need '==' to compare, i.e.
   while(num !=9)
   {
             if (num == 1)
             {
                frac1.Add(frac2); 
                     }
              if (num == 2)
              {
                frac1.Subtract(frac2);  
                      }
               if (num ==3)
               {
                 frac1.Multiply(frac2);  
                       }
               if (num ==4)
               {
                  frac1.Divide(frac2); 
                                                }
      }
     

Open in new window

jkr

Another correction, the menu should be inside the loop:
int main()
{
    
    int n1=0;
    int d1=0;
    int n2=0;
    int d2=0;
    int na=0;
    int da=0;
    int num = 0;
  
      
 
   Fraction frac1(n1,d1);
   Fraction frac2(n2,d2);
 
   while(num !=9)
   {
       cout<<"This program performs operations on fractions"<<endl;
       menu(num,n1,n2,d1,d2); 
             if (num == 1)
             {
                frac1.Add(frac2); 
                     }
              if (num == 2)
              {
                frac1.Subtract(frac2);  
                      }
               if (num ==3)
               {
                 frac1.Multiply(frac2);  
                       }
               if (num ==4)
               {
                  frac1.Divide(frac2); 
                                                }
      }
     
     
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

Open in new window

snyderchip123

ASKER
GREAT! This helped make it run the loop. It is still not doing the math though..



Do I need to change something in the header?
Fraction Fraction::Add(Fraction someFraction) const {
    Fraction result;
    result.num = num * someFraction.denom + denom * someFraction.num;
    result.denom = denom * someFraction.denom;
    return result;

I am attaching my header file to see if there are any errors in this.  I appreciate all your help. You have been wonderful!


#ifndef FRACTION_H
#define FRACTION_H
#include <iostream>
#include <cmath>
 
 
class Fraction {
public:                             
    Fraction();                     
    Fraction(int);                    
    Fraction(int, int);              
    void Read();
    void Write() const;              
    Fraction Add(Fraction) const;     
    Fraction Subtract(Fraction) const;
    Fraction Multiply(Fraction) const;
    Fraction Divide(Fraction) const;  
private:                              
    int num;
    int denom;
};
 
using namespace std;
 
Fraction::Fraction() 
{ 
    num = 0;
    denom = 1;
}
 
Fraction::Fraction(int n) { 
    num = n;
    denom = 1;
}
 
Fraction::Fraction(int n, int d) { 
    num = n;
    denom = d;
}
 
void Fraction::Read() {
    char slash;
    cout << "Enter fraction(numerator / denominator): ";
    cin >> num >> slash >> denom;
    while(slash != '/') {
        cout << "\nERROR! Work with fractions and use numerator / denominator! REENTER!\n";
        cin >> num >> slash >> denom;
    }
}
 
void Fraction::Write() const{ 
    cout << num << " / " << denom ;
}
 
 
 
Fraction Fraction::Add(Fraction someFraction) const { 
    Fraction result;
    result.num = num * someFraction.denom + denom * someFraction.num;
    result.denom = denom * someFraction.denom;
    return result;
}
 
Fraction Fraction::Multiply(Fraction someFraction) const { 
    Fraction result;
    result.num = num * someFraction.num;
    result.denom = denom * someFraction.denom;
    return result;
}
 
Fraction Fraction::Subtract(Fraction someFraction) const { 
    Fraction result;
    result.num = num * someFraction.denom - denom * someFraction.num;
    result.denom = denom * someFraction.denom;
    return result;
}
 
Fraction Fraction::Divide(Fraction someFraction) const { 
    Fraction result;
    result.num = num * someFraction.denom;
    result.denom = denom * someFraction.num;
    return result;
 
};
 
 
#endif

Open in new window

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
jkr

Um, you should manipulate the contents of the object whose method you are calling to see the changes, i.e.
Fraction Fraction::Add(Fraction someFraction) { 
    num = num * someFraction.denom + denom * someFraction.num;
    denom = denom * someFraction.denom;
    return *this;
}

Open in new window

snyderchip123

ASKER
I am not sure what this is doing.  I don't understand?
jkr

Well, that depends on what you want to do - usually, methods are used to manipulate the data of the object they belong to...
âš¡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
snyderchip123

ASKER
I am trying to do this part ADD. Function addFractions: This function adds two fractions together (or you can overload the +operator()).  

I probably should use the operator + but I am not quite sure how to do that..
Would it be something like:  

Or am I changing to much and messing up the rest of my code.. I am so confused right now...


Fraction operator+(const Fraction&) const;
 
And then to define it
 
 Fraction Fraction::operator+(const Fraction){
    Fraction Add();
    result.num = num * someFraction.denom + denom * someFraction.num;
    result.denom = denom * someFraction.denom;
    return result;
}

Open in new window

jkr

'operator+()' should definitely manipulate the object it belongs to and must not be const. Otherwise you'd have to define a global 'operator+()' that takes two arguments, e.g.
 Fraction ::operator+(const Fraction& f1, const Fraction& f2){
    Fraction result;
    result.num = f1.num * f2.denom + f1denom * f2.num;
    result.denom = f1.denom * f2.denom;
    return result;
}

Open in new window

snyderchip123

ASKER
I changed it to look like below so I can use it for Add and use one for my subtract, etc.
It is giving me tons of errors though.

Error      1      error C2039: '+' : is not a member of 'Fraction'      

Error      6      error C2065: 'f1denom' : undeclared identifier      

Error      5      error C2248: 'Fraction::denom' : cannot access private member declared in class 'Fraction'      

Error      3      error C2248: 'Fraction::num' : cannot access private member declared in class 'Fraction'      
Error      11      error C2440: 'return' : cannot convert from 'Fraction' to 'int'      

Error      12      error C2617: '+' : inconsistent return statement      
Error      2      error C4430: missing type specifier - int assumed. Note: C++ does not support default-int      

Fraction::operator+(const Fraction& f1, const Fraction& f2){
    Fraction addResult;
    addResult.num = f1.num * f2.denom + f1denom * f2.num;
    addResult.denom = f1.denom * f2.denom;
    return addResult;
}

Open in new window

This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
snyderchip123

ASKER
Is this still able to be used if I change my operator?

while(num !=9)
   {
       cout<<"This program performs operations on fractions"<
jkr

You need to make the operator a friend of your class
class Fraction;
 
Fraction operator+(const Fraction& f1, const Fraction& f2);
 
class Fraction {
 
friend Fraction operator+(const Fraction& f1, const Fraction& f2);
 
public:                             
    Fraction();                     
    Fraction(int);                    
    Fraction(int, int);              
    void Read();
    void Write() const;              
    Fraction Add(Fraction) const;     
    Fraction Subtract(Fraction) const;
    Fraction Multiply(Fraction) const;
    Fraction Divide(Fraction) const;  
private:                              
    int num;
    int denom;
};
 
Fraction operator+(const Fraction& f1, const Fraction& f2){
    Fraction result;
    result.num = f1.num * f2.denom + f1denom * f2.num;
    result.denom = f1.denom * f2.denom;
    return result;
}

Open in new window

jkr

BTW, then you use it like
    int n1=0;
    int d1=0;
    int n2=0;
    int d2=0;
    int na=0;
    int da=0;
    int num = 0;
  
      
 
   Fraction frac1(n1,d1);
   Fraction frac2(n2,d2);
 
   Fraction result = frac1 + frac2;

Open in new window

âš¡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
snyderchip123

ASKER
Wow it finally clicked in. So I will need to do that with each of my operators.
I will need to make a friend class to subtract, multiply, and divide.

Now would I have to rename the fraction result something different in each on for example to subtract
can I still use

Fraction result = frac 1 - frac 2  ?  

I am leaving work now so I can work on this more,  but I don't know if you will be on when I get home and work on building each of my operators.  I hope you are. You have been such a great help.

I hope I can get the rest of these.

Thanks again!
ASKER CERTIFIED SOLUTION
jkr

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
snyderchip123

ASKER
Ok now my program won't compile. I am getting this error
Error      1      fatal error LNK1168: cannot open C

Also my program wont to the math.. It just runs and also when I enter number 9 it does not exit the program.. Can anyone tell me what I am doing wrong.


#ifndef FRACTION_H
#define FRACTION_H
#include <iostream>
#include <cmath>
 
class Fraction;
 
 
Fraction operator+(const Fraction& f1, const Fraction& f2);
Fraction operator-(const Fraction& f1, const Fraction& f2);
Fraction operator*(const Fraction& f1, const Fraction& f2);
Fraction operator/(const Fraction& f1, const Fraction& f2);
class Fraction {
	friend Fraction operator+(const Fraction& f1, const Fraction& f2);
	friend Fraction operator-(const Fraction& f1, const Fraction& f2);
	friend Fraction operator*(const Fraction& f1, const Fraction& f2);
	friend Fraction operator/(const Fraction& f1, const Fraction& f2);
	
public:                             
    Fraction();                     
    Fraction(int);                    
    Fraction(int, int);              
    void Read();
    void Write() const;              
    Fraction Add(Fraction) const;     
    Fraction Subtract(Fraction) const;
    Fraction Multiply(Fraction) const;
    Fraction Divide(Fraction) const;  
private:                              
    int num;
    int denom;
};
 
using namespace std;
 
 
 
 
Fraction::Fraction() 
{ 
    num = 0;
    denom = 1;
}
 
Fraction::Fraction(int n) { 
    num = n;
    denom = 1;
}
 
Fraction::Fraction(int n, int d) { 
    num = n;
    denom = d;
}
 
void Fraction::Read() {
    char slash;
    cout << "Enter fraction(numerator / denominator): ";
    cin >> num >> slash >> denom;
    while(slash != '/') {
        cout << "\nERROR! Work with fractions and use numerator / denominator! REENTER!\n";
        cin >> num >> slash >> denom;
    }
}
 
void Fraction::Write() const{ 
    cout << num << " / " << denom ;
}
 
 
 
Fraction operator+(const Fraction& f1, const Fraction& f2){
    Fraction result;
    result.num = f1.num * f2.denom + f1.denom * f2.num;
    result.denom = f1.denom * f2.denom;
    return result;
}
 
Fraction operator*(const Fraction& f1, const Fraction& f2){ 
    Fraction result;
    result.num = f1.num * f2.num;
    result.denom = f1.denom * f2.denom;
    return result;
}
 
Fraction operator-(const Fraction& f1, const Fraction& f2){
    Fraction result;
    result.num = f1.num * f1.denom - f2.denom * f2.num;
    result.denom = f1.denom * f2.denom;
    return result;
}
 
Fraction operator/(const Fraction& f1, const Fraction& f2){ 
    Fraction result;
    result.num = f1.num * f1.denom;
    result.denom = f2.denom * f2.num;
    return result;
 
};
 
 
#endif
 
main
 
 
#include <iostream>
#include <cmath>
#include <iomanip>
#include "Fraction.h"
using namespace std;
 
 void menu(int&,int&,int&,int&,int&); 
 
int main()
{
    
    int n1=0;
    int d1=0;
    int n2=0;
    int d2=0;
    int na=0;
    int da=0;
    int num = 0;
  
      
 
   Fraction frac1(n1,d1);
   Fraction frac2(n2,d2);
 
   while(num !=9)
   {
       cout<<"This program performs operations on fractions"<<endl;
       menu(num,n1,n2,d1,d2); 
             if (num == 1)
             {
         		Fraction result = frac1 + frac2;
 
                     }
              if (num == 2)
              {
                Fraction result = frac1 - frac2;
                      }
               if (num ==3)
               {
                 Fraction result = frac1 * frac2;  
                       }
               if (num ==4)
               {
                 Fraction result = frac1 / frac2;
                       }
      }
     
     
    
    system("PAUSE");
    return EXIT_SUCCESS;
}
 
    void menu(int& num,int& d1,int& d2,int& n1,int& n2)
    {
 
     
    
      
  cout<<"Enter:"<<endl;
   cout<<"1 :To add a fraction"<<endl;
   cout<<"2 :To subtract a fraction"<<endl;
   cout<<"3 :To multiply a fraction"<<endl;
   cout<<"4 :To divide a fraction"<<endl;
   cout<<"9 :To exit the program"<<endl;
   cin>>num;
   cout<<"For fraction 1"<<endl;
   cout<<"Enter the numerator"<<endl;
   cin>>n1;   
   cout<<"Enter the denominator"<<endl;
   cin>>d1;
   
   cout<<"For fraction 2"<<endl;
   cout<<"Enter the numerator"<<endl;
   cin>>n2;
   
   cout<<"Enter the denominator"<<endl;
   cin>>d2;
        }

Open in new window

snyderchip123

ASKER
Thank you for all the help
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck