My mixed int is getting set to the wrong value somewhere. Can anyone help me see where?
Driver:
#include <iostream>
using std::cout;
using std::endl;
using std::cin;
#include "Rational.h"
int main()
{
int mixed1 = 0;
int numerator1 = 0;
int denominator1 = 0;
cout << "Enter first numerator and denominator to add, subtract, multiply and divide: ";
cin >> numerator1 >> denominator1;
Rational f(mixed1, numerator1, denominator1);//Create Rational object f
cout << "Enter second numerator and denominator to add, subtract, multiply and divide: ";
cin >> numerator1 >> denominator1;
Rational g(mixed1, numerator1, denominator1);//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;
}
header:
#ifndef RATIONAL_H
#define RATIONAL_H
class Rational
{
public:
Rational(int =1, int =1, int = 1); // 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
#include
functions:
<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 mixed, 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;
}
while (numerator > denominator)
{
mixed++;
numerator -= denominator;
}
for (int i = numerator; i > 1; i--)
{
if ( numerator%i == 0 && denominator%i ==0)
{
numerator = numerator/i;
denominator = denominator/i;
}
}
}
Rational Rational::operator+(const Rational &x ) const
{
return Rational(mixed + x.mixed, numerator * x.denominator + x.numerator * denominator,
denominator * x.denominator);
}
Rational Rational::operator-(const Rational &x) const
{
return Rational(mixed - x.mixed, numerator * x.denominator - x.numerator * denominator,
denominator * x.denominator);
}
Rational Rational::operator*(const Rational &x) const
{
return Rational(mixed * x.mixed, numerator * x.numerator, denominator * x.denominator);
}
Rational Rational::operator /(const Rational &x) const
{
return Rational(mixed / x.mixed, numerator * x.denominator, denominator * x.numerator);
}
void Rational::printRational()
{
if (mixed == 0)
cout << numerator << "/" << denominator;
else
{
cout << mixed << " " << numerator << "/" << denominator;
}
}
Thanks for the help:lol: