Debugging issue regarding fractions

The code below is designed to add, subtract, multiply, and divide fractions created in the class Rational. The class definition is in a header file and is fine. Also, int main() was supplied, and must not be changed. The two problems found are:

1. the output of the functions (i.e. what x equals) is always constant, being equal to the second fraction (i.e. "d").

2. The first fraction in all functions following addition() is "1/8", or , in general "1/[denominator of d]".

Here is the code in question

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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
void Rational::printRational() // prints a single fraction
{
   cout << numerator << "/" << denominator;
}

void Rational::printRationalAsDouble() // prints a fraction as a decimal
{
   double function = static_cast <double> (numerator)/static_cast <double> (denominator);
   cout << function;
}

Rational Rational::addition(Rational y) // adds fractions
{
   int numerSum;
   numerSum = (numerator * y.getDenominator()) + (denominator * y.getNumerator());
   if (denominator <= y.getDenominator())
   {
       denominator = y.getDenominator();
   }
   return y;
}

Rational Rational::subtraction(Rational y) // subtracts fractions
{
   int numerDifference;
   numerDifference = (numerator * y.getDenominator()) - (denominator * y.getNumerator());
   if (y.getDenominator() >= denominator)
   {
       y.setDenominator(denominator);
   }
   return y;
}

Rational Rational::multiplication(Rational y) // multiples fractions
{
   int numerProduct;
   int numerProduct2;
   numerProduct = (numerator * y.getNumerator());
   numerProduct2 = (denominator * y.getDenominator());
   return y;
}

Rational Rational::division(Rational y) // divides fractions
{
   int numerQuotient;
   int numerQuotient2;
   numerQuotient = (numerator * y.getDenominator());
   numerQuotient2 = (denominator * y.getNumerator());
   return y;
}

int main()
{
   Rational c( 3, 6 );
   Rational d( 7, 9 );
   Rational x; // creates three rational objects 
   
   c.reduction();
   d.reduction();

   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;
}


Any and all help will be welcomed.
This function

1
2
3
4
5
6
7
8
9
10
Rational Rational::addition(Rational y) // adds fractions
{
   int numerSum;
   numerSum = (numerator * y.getDenominator()) + (denominator * y.getNumerator());
   if (denominator <= y.getDenominator())
   {
       denominator = y.getDenominator();
   }
   return y;
}


has no any sense. The function gets as an argument an object of type Rational and returns the same object without any changes of it. So what you will pass to the function as an argument the same will be returned by the function.

Apart from this as far as I know denominator of the sum is calculated as a product of two denominators.
Last edited on
Topic archived. No new replies allowed.