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:

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;
}
```

```
#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;
}
```

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;
}
```

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;
}
```

```
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);
}
}
```

```
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;
}
```

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

```
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;
}
```

```
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;
}
```

```
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;
```

This one is on us!

(Get your first solution completely free - no credit card required)

UNLOCK SOLUTION
Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.