Thank you, that all helps to fill in the banks. I started doing some tests on it and I can take user input numerator/denominator and it produces the right output with some tests and the wrong output with some other tests. I am trying to understand why so I can address the issue.
Output that works good:
Enter a numerator/denominator: 5/2
2+1/2
Enter a numerator/denominator: 1/2
1/2
Enter a numerator/denominator: -1/2
-1/2
Output that does not work good:
Enter a numerator/denominator: 5/-2
-2+1/-2
Enter a numerator/denominator: 1/-2
0+1/-2
Enter a numerator/denominator: -1/-2
0+-1/-2
I have defined my overloaded << function so that it outputs all these examples correctly from fraction objects with hard coded parameters, but when I use the cin >> fraction object from user input in main then then some are wrong.
Here, I get an error with the red squiggly line under the first >> in the if statement. Same error as above.
1 2 3 4 5 6 7 8 9 10 11 12 13
|
std::istream& operator >> (std::istream& strm, Fraction& right)
{
char slash;
int numerator, denominator;
strm >> right.numerator >> slash >> right.denominator;
if (right.numerator < 0 && right.denominator < 0) {
strm >> abs(right.numerator) >> slash >> abs(right.denominator);
}
return strm;
}
|