Home > Enterprise >  What may be the reason for this constructor failing to initialize with the given values?
What may be the reason for this constructor failing to initialize with the given values?

Time:10-02

I have been trying to solve an example question from a book and I encountered this problem while initializing the constructor with the values given below. Normally constructor initializes the variables beforehand. When I run the function from a function like Rational_Caller, the member function file given below gives an error as "DIVISION BY ZERO ERROR". Since I initialize the first object with given values in Rational_Caller function, I couldn't figure out why denominator gets the value 0.

this is the header file

// Rational Class header file

#ifndef _RATIONAL_H_
#define _RATIONAL_H_

class Rational
{
public:
    Rational(int = 0, int = 1); // default constructor
    Rational addition(const Rational&); 
    Rational subtraction(const Rational&); 
    Rational multiplication(const Rational&); 
    Rational division(const Rational&); 
    void printRational(); 
    void printRationalAsdouble(); 

private:
    int numerator; 
    int denominator; 
    void reduction(); // function to reduce using great common divisor 

}; 




#endif // !_RATIONAL_H_

this is the member function definitions for the file Rational.h


#include <iostream>
#include "Rational.h" // include definiton of class Rational 

using namespace std; 

Rational::Rational(int n, int d)
{
    numerator = n; 
    denominator = d; 
    reduction(); 
} 


Rational Rational::addition(const Rational& a)
{
    Rational t; 
    
    t.numerator = a.numerator * denominator; 
    t.numerator  = a.denominator * numerator; 
    t.denominator = a.denominator * denominator; 
    t.reduction();
    return t; 

} 

Rational Rational::subtraction(const Rational& s)
{
    Rational t;
    t.numerator = s.numerator * denominator; 
    t.numerator -= denominator * s.numerator; 
    t.denominator = s.denominator * denominator; 
    return t; 

}  

Rational Rational::multiplication(const Rational& m)
{
    Rational t; 
    t.numerator = m.numerator * numerator; 
    t.denominator = m.denominator * denominator; 
    t.reduction(); 
    return t;   
} 

Rational Rational::division(const Rational& v)
{
    Rational t; 
    t.numerator = numerator * v.denominator; 
    t.denominator = denominator * v.numerator; 
    t.reduction(); 
    return t; 

} 


void Rational::printRational()
{
    if (denominator == 0) 
        cout << "\nDIVIDE BY ZERO ERROR!!!" << "\n";
    else if (numerator == 0) 
        cout << 0;
    else
        cout << numerator << '/' << denominator; 
} 


void Rational::printRationalAsdouble()
{
    cout << static_cast<double>(numerator) / denominator; 
} 

void Rational::reduction()
{
    int largest; 

    largest = numerator > denominator ? numerator : denominator; 

    int gcd = 0; 

    for (int loop = 2; loop <= largest; loop  )
    {
        if (numerator % loop == 0 && denominator % loop == 0)
            gcd = loop; 

        if (gcd != 0)
        {
            numerator /= gcd; 
            denominator /= gcd; 
        } 

    }

} 

this is the file I call the functions

#include <iostream>
using namespace std; 

#include "Rational.h" 

int main()
{
    Rational obj1(8, 14), obj2(5, 7), resobj; 

    obj1.printRational(); 
    cout << "   ";
    obj2.printRational(); 
    resobj = obj1.addition(obj2); 

    cout << " = "; 
    resobj.printRational(); 
    


} 

CodePudding user response:

The proble is in void Rational::reduction() function.

What happens when if (numerator % loop == 0 && denominator % loop == 0) is false?

You should have initlise gcd to 0 for each iteration of for-loop or even batter, do't use gcd variable at all. See it here in action:

for (int loop = 2; loop <= largest; loop  )
{
    if (numerator % loop == 0 && denominator % loop == 0)
    {
        numerator /= loop; 
        denominator /= loop; 
    } 
}
  •  Tags:  
  • c
  • Related