0

I am having some trouble with the logic for my constructor. The constructor is supposed to open a object of class Rational. The constructor is also supposed to check to make sure the denominator is not zero, the denominator is not negative, and either reduce the fraction or make it into a reduced mixed number. I had it halfway working but now I have a major logic error that I just can't find. Any help would be greatly appreciated!

I provided all the code below.

```
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
using std::fixed;
#include <iomanip>
using std::setprecision;
#include "Rational.h" //includes Rational class definition
Rational::Rational( int numeratorPart, int denominatorPart) //constructor, initialize fraction array to 3, 4
{
denominator = denominatorPart;
numerator = numeratorPart;
mixed = 0;
if (denominator == 0)
{
cout << "invalid denominator please enter a number other than 0: ";
cin >> denominator;
}
else
denominator = denominatorPart;
if (denominator < 0)
{
denominator = denominator * -1;
numerator = numerator * -1;
}
if (numerator > denominator )
{
if (denominator%numerator==0)
{
mixed = numerator/denominator;
numerator = 1;
denominator = 1;
}
else
{
for (int i = 1; numerator >= (denominator*i); i++)
{
mixed = i;
numerator = numerator%denominator;
}
}
}
if (numerator%denominator == 0)
{
for (int i=2; denominator > numerator*i; i++)
{
while ( (numerator*i)%denominator==0)
numerator = numerator/i;
denominator = denominator/i;
}
}
}
Rational Rational::operator+(const Rational &x ) const
{
return Rational(numerator * x.denominator + denominator * x.denominator,
denominator * x.denominator);
}
Rational Rational::operator-(const Rational &x) const
{
return Rational(numerator * x.denominator - denominator * x.denominator,
denominator * x.denominator);
}
Rational Rational::operator*(const Rational &x) const
{
return Rational(numerator * x.numerator, denominator * x.denominator);
}
Rational Rational::operator /(const Rational &x) const
{
return Rational(numerator * x.denominator, denominator * x.numerator);
}
void Rational::printRational()
{
if (mixed == 0)
cout << numerator << "/" << denominator;
else
{
cout << mixed << " " << numerator << "/" << denominator;
}
}
```

The header file:

```
#ifndef RATIONAL_H
#define RATIONAL_H
class Rational
{
public:
Rational(int =0, int = 0); // constructor
Rational operator+(const Rational & ) const;
Rational operator-(const Rational & ) const;
Rational operator*(const Rational & ) const;
Rational operator/(const Rational & ) const;
void printRational();
private:
int numerator;
int denominator;
int mixed;
float decimalAnswer;
};
#endif
```

The driver file:

```
#include <iostream>
using std::cout;
using std::endl;
#include "Rational.h"
int main()
{
Rational f(1, 2);//Create Rational object f
Rational g(3, 4);//Create Rational object g
Rational a;
cout << "f: ";
f.printRational();
cout << "\ng: ";
g.printRational();
cout << "\na: ";
a.printRational();
a = f + g;
cout << "\n\na = f + g" << endl;
a.printRational();
cout << " = ";
f.printRational();
cout << " + ";
g.printRational();
a = f - g;
cout << "\n\na = f - g" << endl;
a.printRational();
cout << " = ";
f.printRational();
cout << " - ";
g.printRational();
a = f * g;
cout << "\n\na = f * g" << endl;
a.printRational();
cout << " = ";
f.printRational();
cout << " * ";
g.printRational();
a = f / g;
cout << "\n\na = f / g" << endl;
a.printRational();
cout << " = ";
f.printRational();
cout << " / ";
g.printRational();
return 0;
}
```

Thanks again for any help.