troubleshooting Question

C++Programming Languages-Other

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;
}
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.