### Help with calling the reduce function

The program works, but the rational number doesn't print as reduced. I guess i have to call the reduction function, but i'm not sure where. can someone help?
the output that i get is:
2/6 + 7/8 = 58/48
58/48 = 1.20833

2/6 - 7/8 = -26/48
-26/48 = -0.541667

2/6 x 7/8 = 14/48
14/48 = 0.291667

2/6 / 7/8 = 16/42
16/42 = 0.380952

the output that i suppose to get is:
1/3 + 7/8 = 29/24
29/24 = 1.20833

1/3 - 7/8 = -13/24
-13/24 = -0.541667

1/3 x 7/8 = 7/24
7/24 = 0.291667

1/3 / 7/8 = 8/21
8/21 = 0.380952

 ``12345678910111213141516171819202122232425`` ``````//Prevent multiple inclusions of header #ifndef RATIONAL_H #define RATIONAL_H //Rational class definition class Rational { public: Rational(int, int);//constructor Rational(); void reduction(); //function to reduce the rational number Rational addition(Rational &); //add rational number Rational subtraction(Rational &); //subtract rational number Rational multiplication(Rational &); // multiply rational number Rational division(Rational &); // divide rational number void printRational(); //print rational number void printRationalAsDouble(); //print rational number as decimal private: int numerator; int denominator; }; #endif ``````

here is my source file
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131`` ``````#include using namespace std; #include "Rational.h" // include definition of class Rational void Rational::reduction() { int largest; largest = numerator > denominator ? numerator : denominator; int gcd = 0; // greatest common divisor for ( int loop = 2; loop <= largest; loop++ ) if ( numerator % loop == 0 && denominator % loop == 0 ) gcd = loop; if (gcd != 0) { numerator /= gcd; denominator /= gcd; } // end if } // end function reduction Rational::Rational(int a, int b) { numerator = a; denominator = b; } Rational::Rational() { } Rational Rational::addition(Rational &a) { int newDenominator = denominator * a.denominator; int newNumerator = (numerator * a.denominator) + (denominator * a.numerator); Rational newValue(newNumerator, newDenominator); return newValue; } Rational Rational::subtraction(Rational &a) { int newDenominator = denominator * a.denominator; int newNumerator = (numerator * a.denominator) - (denominator * a.numerator); Rational newValue(newNumerator, newDenominator); return newValue; } Rational Rational::multiplication(Rational &a) { int newDenominator = denominator * a.denominator; int newNumerator = numerator * a.numerator; Rational newValue(newNumerator, newDenominator); return newValue; } Rational Rational::division(Rational &a) { int newDenominator = denominator * a.numerator; int newNumerator = numerator * a.denominator; Rational newValue(newNumerator, newDenominator); return newValue; } void Rational::printRational() { cout << numerator << "/" << denominator; } void Rational::printRationalAsDouble() { cout << static_cast(numerator)/denominator; } int main() { Rational c( 2, 6 ), d( 7, 8 ), x; // creates three rational objects c.printRational(); // prints rational object c cout << " + "; d.printRational(); // prints rational object d x = c.addition( d ); // adds object c and d; sets the value to x cout << " = "; x.printRational(); // prints rational object x cout << '\n'; x.printRational(); // prints rational object x cout << " = "; x.printRationalAsDouble(); // prints rational object x as double cout << "\n\n"; c.printRational(); // prints rational object c cout << " - "; d.printRational(); // prints rational object d x = c.subtraction( d ); // subtracts object c and d cout << " = "; x.printRational(); // prints rational object x cout << '\n'; x.printRational(); // prints rational object x cout << " = "; x.printRationalAsDouble(); // prints rational object x as double cout << "\n\n"; c.printRational(); // prints rational object c cout << " x "; d.printRational(); // prints rational object d x = c.multiplication( d ); // multiplies object c and d cout << " = "; x.printRational(); // prints rational object x cout << '\n'; x.printRational(); // prints rational object x cout << " = "; x.printRationalAsDouble(); // prints rational object x as double cout << "\n\n"; c.printRational(); // prints rational object c cout << " / "; d.printRational(); // prints rational object d x = c.division( d ); // divides object c and d cout << " = "; x.printRational(); // prints rational object x cout << '\n'; x.printRational(); // prints rational object x cout << " = "; x.printRationalAsDouble(); // prints rational object x as double cout << endl; system("pause"); return 0; } // end main ``````
You could put `newValue.reduction();` before all your `return newValue;` in your Rational arithmetic methods.

Or, you could just call `reduction();` on the first line of your `printRational()` method.
Last edited on
I have tried these two before and it didn't work it still doesn't reduce the rational numbers
never mind i guess i was thinking to hard actually i was trying reduction() by itself which was my error. It suppose to be Rational::reduction(); I tried so many syntax by the end i wasn't sure anymore.
I change my code to this:
 ``123456`` ``````void Rational::printRational() { Rational::reduction(); cout << numerator << "/" << denominator; }``````

and now it works thank for the insight
Topic archived. No new replies allowed.