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

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

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 Write() 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;
}

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

while(num !=9)
{
if (num = 1)
{
}
if (num =2)
{
Subtract(Fraction);
}
if (num =3)
{
Multiply(Fraction);
}
if (num =4)
{
Divide(Fraction);
}
}

system("PAUSE");
return EXIT_SUCCESS;
}

{
int num;
int n1;
int n2;
int d1;
int d2;

cout<<"Enter:"<<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;
}
``````
C++Programming Languages-Other

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 Write() 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;
}

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

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;

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

{
int num;
int n1;
int n2;
int d1;
int d2;

cout<<"Enter:"<<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;
}
``````
snyderchip123

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;

Fraction frac1(n1,d1);
Fraction frac2(n2,d2);

while(num !=9)
{
if (num = 1)
{
}
if (num =2)
{
frac1.Subtract(frac2);
}
if (num =3)
{
frac1.Multiply(frac2);
}
if (num =4)
{
frac1.Divide(frac2);
}
}

system("PAUSE");
return EXIT_SUCCESS;
}
``````
snyderchip123

Ok now I am getting the hang of this. Â So this code means
but does this call my operator? Â I think it is by calling fraction two in () correct?

Â  Â  Fraction result;
Â  Â  result.num = num * someFraction.denom + denom * someFraction.num;
Â  Â  result.denom = denom * someFraction.denom;
Â  Â  return result;
}

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.

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;

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;

Fraction frac1(n1,d1);
Fraction frac2(n2,d2);

while(num !=9)
{
if (num = 1)
{
}
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<<"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;
}
``````
snyderchip123

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
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)
{
}
if (num == 2)
{
frac1.Subtract(frac2);
}
if (num ==3)
{
frac1.Multiply(frac2);
}
if (num ==4)
{
frac1.Divide(frac2);
}
}

``````
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;
if (num == 1)
{
}
if (num == 2)
{
frac1.Subtract(frac2);
}
if (num ==3)
{
frac1.Multiply(frac2);
}
if (num ==4)
{
frac1.Divide(frac2);
}
}

system("PAUSE");
return EXIT_SUCCESS;
}
``````
snyderchip123

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 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 Write() 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;
}

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

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...
snyderchip123

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){
result.num = num * someFraction.denom + denom * someFraction.num;
result.denom = denom * someFraction.denom;
return result;
}
``````
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;
}
``````
snyderchip123

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){
addResult.num = f1.num * f2.denom + f1denom * f2.num;
}
``````
snyderchip123

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

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!
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.
snyderchip123

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 Write() 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;
}

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;

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