Good Afternoon,
I'm having problems with my program that deals with fractions. The problems are with the functions multiply and divide that are not working correctly. So far I have this code.
CPP FILE:
#include <iostream>
#include <cmath>
#include "hw4_head.h"
using namespace std;
int main ()
{
int a, b, c, d, e, f, gcd;
int option;
char op;
char answer = 'y';
while (answer != 'n')
{
showMenu();
cout <<"Enter a choice ";
cin>>option;
switch(option)
{
case 1:
cout <<"Going to perform the ADD operation on a fraction question"<<endl;
cout << "Please enter a fraction question \n"
<<"in the form of a / b operation c / d ";
getFract (a, b);
op = getOperator ();
getFract (c, d);
disPlayFract (a, b);
cout << " " << op << " ";
disPlayFract (a, d);
cout <<endl;
Add (a, b, c, d, e, f);
cout<<"The answer is equal to"<<endl;
disPlayFract (e, f);
cout <<endl;
gcd = gcf (e, f);
e = e / gcd;
f = f / gcd;
cout <<"The answer in reduce form is"<<endl;
disPlayFract (e, f);
cout<<endl;
break;
case 2:
cout <<"Going to perform the SUBSTRACT operation on a fraction question"<<endl;
cout << "Please enter a fraction question \n"
<<"in the form of a / b operation c / d ";
getFract (a, b);
op = getOperator ();
getFract (c, d);
disPlayFract (a, b);
cout << " " << op << " ";
disPlayFract (a, d);
cout <<endl;
Substract (a, b, c, d, e, f);
cout<<"The answer is equal to"<<endl;
disPlayFract (e, f);
cout <<endl;
gcd = gcf (e, f);
e = e / gcd;
f = f / gcd;
cout <<"The answer in reduce form is"<<endl;
disPlayFract (e, f);
cout<<endl;
break;
case 3:
cout <<"Going to perform the DIVIDE operation on a fraction question"<<endl;
cout << "Please enter a fraction question \n"
<<"in the form of a / b operation c / d ";
getFract (a, b);
op = getOperator ();
getFract (c, d);
disPlayFract (a, b);
cout << " " << op << " ";
disPlayFract (a, d);
cout <<endl;
Divide (a, b, c, d, e, f);
cout<<"The answer is equal to"<<endl;
disPlayFract (e, f);
cout <<endl;
gcd = gcf (e, f);
e = e / gcd;
f = f / gcd;
cout <<"The answer in reduce form is"<<endl;
disPlayFract (e, f);
cout<<endl;
break;
case 4:
cout <<"Going to perform the MULTIPLY operation on a fraction question"<<endl;
cout << "Please enter a fraction question \n"
<<"in the form of a / b operation c / d ";
getFract (a, b);
op = getOperator ();
getFract (c, d);
disPlayFract (a, b);
cout << " " << op << " ";
disPlayFract (a, d);
cout <<endl;
Multiply (a, b, c, d, e, f);
cout<<"The answer is equal to"<<endl;
disPlayFract (e, f);
cout <<endl;
gcd = gcf (e, f);
e = e / gcd;
f = f / gcd;
cout <<"The answer in reduce form is"<<endl;
disPlayFract (e, f);
cout<<endl;
break;
default:
cout <<"You entered an invalid input"<<endl;
}
cout << "Would you like to workout another problem (y/n) ";
cin>>answer;
}
cout <<"Good Bye."<<endl;
return 0;
}
Header file:
#include <iostream>
#include <cmath>
using namespace std;
#ifndef HW4_HEAD_H
#define HW4_HEAD_H
void getFract( int& a, int& b)
{
char dummy;
cin>>a>>dummy>>b;
}
char getOperator ()
{
char op;
cin>>op;
return op;
}
void disPlayFract (const int a, const int b)
{
cout << a << " / "<< b;
}
void Add (const int a, const int b, const int c, const int d, int& e, int& f)
{
e = a * d + b * c;
f = b * d;
}
void Substract (const int a, const int b, const int c, const int d, int& e, int& f)
{
e = a * d - b * c;
f = b * d;
}
void Divide (const int a, const int b, const int c, const int d, int& e, int& f)
{
e = (a * d) / (c *b);
f = c * d;
}
void Multiply (const int a, const int b, const int c, const int d, int& e, int& f)
{
e = (a / d) * (b / c);
f = b * d;
}
int gcf (int A, int B)
{
int R;
R = A % B;
while (R != 0)
{
A = B;
B = R;
R = A % B;
}
return B;
}
void showMenu()
{
cout<<"**************Fractional Arithmetic Program Menu***********************"<<endl;
cout<<"Enter 1 to perform ADD operation on a fraction question"<<endl;
cout<<"Enter 2 to perform SUBSTRACT operation on a fraction question"<<endl;
cout<<"Enter 3 to perform DIVIDE operation on a fraction question"<<endl;
cout<<"Enter 4 to perform MULTIPLY operation on a fraction question"<<endl;
}
#endif
I really appreciate any help that I can get.