I had to write a fraction calculator program and my only problem (I think) is when I input the two fractions it gives me garbage - for instance if I put 1/2 + 1/2 it gives me 1374389536/687194768 - can anyone help me correct this please?
Thanks!
divisionByZero.h
#include <iostream>
#include <string>
using namespace std;
class divisionByZero
{
public:
divisionByZero()
{
message = "Division by zero";
}
divisionByZero(string str)
{
message = str;
}
string what()
{
return message;
}
private:
string message;
};
main.ccp
#include <iostream>
#include "divisionByZero.h"
using namespace std;
void doOperation(int& a, int& b, int& c,int& d) throw (divisionByZero);
void addFractions(int& a, int& b, int& c,int& d, int& num, int& den);
void subtractFractions(int& a, int& b, int& c,int& d, int& num, int& den);
void multiplyFractions(int& a, int& b, int& c,int& d, int& num, int& den);
void divideFractions(int& a, int& b, int& c,int& d, int& num, int& den);
void exit();
void menu(char operationType);
int main()
{
int a=0,b=0,c=0,d=0;
try
{
doOperation(a, b, c, d);
}
catch (divisionByZero divByZeroObj)
{
cout << divByZeroObj.what() << endl;
}
return 0;
}
void doOperation(int& a, int& b, int& c,int& d) throw (divisionByZero)
{
char operationType = 'a';
try
{
cout << "Enter the numerator for the first fraction: ";
cin >> a;
cout << "Enter the denominator for the first fraction: ";
cin >> b;
cout << "Enter the numerator for the second fraction: ";
cin >> c;
cout << "Enter the denominator for the second fraction: ";
cin >> d;
if (b == 0 || d == 0)
throw divisionByZero();
menu(operationType);
}
catch (divisionByZero)
{
throw
divisionByZero("Denominator can not be zero!");
}
}
void menu(char operationType)
{
int a,b,c,d,num,den;
cout << "Please choose the operation you would like to perform:"<< endl;
cout << " + (addition)\n"
<< " - (subtraction)\n"
<< " * (multiplication)\n"
<< " / (division)\n"
<< " x (exit)\n"
<< "Operation to perform: ";
cin >> operationType;
cout << endl;
switch (operationType)
{
case '+':
addFractions(a,b,c,d,num,den);
cout << "\nThe answer is " << num << "/" << den << endl;
break;
case '-':
subtractFractions(a,b,c,d,num,den);
cout << "\nThe answer is " << num << "/" << den << endl;
break;
case '*':
multiplyFractions(a,b,c,d,num,den);
cout << "\nThe answer is " << num << "/" << den << endl;
break;
case '/':
divideFractions(a,b,c,d,num,den);
cout << "\nThe answer is " << num << "/" << den << endl;
break;
case 'x':
exit();
break;
default:
cout << "Your input was invalid.\n"<< endl;
}
}
//function to add fractions
void addFractions(int& a, int& b, int& c,int& d, int& num, int& den)
{
num = (a*d + b*c);
den = (b*d);
}
//function to subtract fractions
void subtractFractions(int& a, int& b, int& c,int& d, int& num, int& den)
{
num = ((a*d) - (b*c));
den = (b*d);
}
//function to multiply fractions
void multiplyFractions(int& a, int& b, int& c,int& d, int& num, int& den)
{
num = (a*c);
den = (b*d);
}
//function to divide fractions
void divideFractions(int& a, int& b, int& c,int& d, int& num, int& den)
{
num = (a*d);
den = (b*c);
}
void exit()
{
cout << "Thank you for using the Fraction Calculator." <<endl;
}