Output for Struct is not turning out right!
Apr 13, 2014 at 10:16pm UTC
Below is the output:
Input the first numerator: 1
Input the first denominator: 2
Is the fraction positive? (Y or N): Y
Input the second numerator: 1
Input the second denominator: 2
Is the fraction positive? (Y or N): N
Result is: 1/4
I want 1/4 to become a negative with a "-" in front if it. I keep getting positives when I indicate that one of the fraction is a negative. Any help is appreciated. Thanks!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
#include <cstdlib>
#include <iostream>
using namespace std;
struct fraction
{
int num;
int denom;
bool positive;
};
struct fraction fracMult(struct fraction &f1, struct fraction &f2, struct fraction &fresult);
int main()
{
fraction f1, f2, fresult;
char tempchar;
cout << "Input the first numerator: " ;
cin >> f1.num;
cout << "Input the first denominator: " ;
cin >> f1.denom;
cout << "Is the fraction positive? (Y or N): " ;
cin >> tempchar;
if (tempchar = 'Y' )
{
f1.positive = true ;
}
else
{
f1.positive = false ;
}
cout << "Input the second numerator: " ;
cin >> f2.num;
cout << "Input the second denominator: " ;
cin >> f2.denom;
cout << "Is the fraction positive? (Y or N): " ;
cin >> tempchar;
if (tempchar = 'Y' )
{
f2.positive = true ;
}
else
{
f2.positive = false ;
}
fracMult(f1, f2, fresult);
cout << "Result is: " ;
if (fresult.positive = false )
{
cout << "-" ;
}
cout << fresult.num << "/" << fresult.denom << endl;
return 0;
}
struct fraction fracMult(struct fraction &f1, struct fraction &f2, struct fraction &fresult)
{
fresult.num = f1.num * f2.num;
fresult.denom = f1.denom * f2.denom;
if (f1.positive == f2.positive)
{
fresult.positive = true ;
}
else
{
fresult.positive = false ;
}
return fresult;
}
Apr 13, 2014 at 10:48pm UTC
All of your if
statements should have == instead of =.
Topic archived. No new replies allowed.