i'm having problem in my code.. displayFraction() doesn't display the right answer... any suggestions please
here's the code
#include <iostream>
using namespace std;
int scanFraction(int x,int y, char z);
char getOperator(char w);
int addFractions(int x,int a,int y,int b, char w);
int multiplyFractions(int x,int a,int y,int b, char w);
int findGCD(int c,int d);
int reduceFraction(int c, int d, int e);
int displayFraction(int f, int g, int x, int a, int y, int b, char w);
void main(void)
{
int numA,denA; //the numerator and the denominator of the first fraction
int numB,denB; //the numerator and the denominator of the second fraction
char operators, //the arithmetic operator that should be used
slash, //the slash (/) symbol
ans; //whether the user wants to perform another calculation or not
cout <<"THIS PROGRAM PERFORMS ARITHMETIC OPERATIONS ON FRACTIONS:\n";
cout <<"by: Henna Mae Bataller\n";
do
{
cout << "Enter first fraction as two integers separated by a slash: ";
cin >>numA >>slash >>denA;
scanFraction(numA, denA, slash);
cout <<"Enter an arithmetic operator (+,-,*, or /): ";
cin >> operators;
getOperator(operators);
cout <<"Enter second fraction as two integers separated by slash: ";
cin >>numB >>slash >>denB;
cout <<endl;
scanFraction(numB, denB, slash);
if (operators == '+')
addFractions(numA,denA,numB,denB,operators);
else if (operators == '-')
addFractions(numA,denA,numB*-1,denB,operators);
else if (operators == '*')
multiplyFractions(numA,denA,numB,denB,operators);
else if (operators == '/')
multiplyFractions(numA,denA,numB,denB,operators);
cout <<"\n\nPerform another arithmetid calculation, [y/n]?";
cin >>ans;
}while (ans=='Y' && ans=='y');
}
int scanFraction(int x,int y, char z)
{
using namespace std;
if (z ==0)
cout <<"\tInvalid, separate numerator and denominator!\n";
else if (y<=0)
cout <<"\tInvalid, denominator must be nonnegative!\n";
else if (x==0 && y<=0)
cout <<"\tInvalid, please read directions carefully!\n";
return 0;
}
if (c%d==0)
return d;
else
return findGCD(d,c%d);
}
int reduceFraction(int c, int d, int e)
{
using namespace std;
int reduceA, reduceB, x, a, y, b;
char w;
reduceA=c/e;
reduceB=d/e;
displayFraction(reduceA, reduceB, x, a, y, b, w);
return 0;
}
int displayFraction(int f, int g, int x, int a, int y, int b, char w)
{
cout <<"\tReduced form (applying GCD): " <<x <<"/" <<a <<w <<" " <<y <<"/" <<b <<" = " <<f <<"/" <<g;
return 0;
}
//Reduced form (applying GCD): here the answer is -858993460 something like that...
please tell me whats missing and whats wrong with this code
First of all, please edit your post so that it uses code tags - the <> formatting button on the right.
Now for the problems I can see so far:
1.
void main(void)
The format should be :
1 2 3 4 5 6
voidint main(void) {
//your code here
return 0; //if all is well, something else if not
} //end of main function
2. With this part:
1 2
int numA,denA; //the numerator and the denominator of the first fraction
int numB,denB; //the numerator and the denominator of the second fraction
The type is int, which won't work for division, as integer division truncates, this is the main problem with your program.
3. Don't do this - even though it may compile:
1 2 3
char operators, //the arithmetic operator that should be used
slash, //the slash (/) symbol
ans; //whether the user wants to perform another calculation or not
Do this instead:
1 2 3 4 5
//It is best to declare one variable per line
char operators; //the arithmetic operator that should be used
char slash; //the slash (/) symbol this variable not needed
char ans; //whether the user wants to perform another calculation or not
4. scanFraction isn't a good name for this function - a good name refelects what the function does, what about ValidateInput
5. The getOperator function:
1 2 3 4 5 6 7 8 9 10 11 12 13
char getOperator(char w) //think of a better name as above
{
usingnamespace std; //no need for this again
if (w<=0 && w>=0) //doesn't make sense at all - less than 0 and greater than 0
{
cout <<"\t" <<w <<" is invalid, re-enter operator(+,-,*,/): ";
cin >>w;
return w;
}
elsereturn w;
}
6. With this do loop condition:
}while (ans=='Y' && ans=='y'); //always false
You would be much better to make use of the toupper function, so you don't have to test the value of ans twice, so you won't need the && (and operator) which is the wrong one in this case.
There are probably other problems with the code, but this should be enough to go on with.