Calculations in class not outputting result

  c++, class, function

I have an assignment where I have to add/subtract/multiply/divide a set of two numbers in a form of a fraction so a/b + c/d would be a * d + b * c / b * d. I’ve got the code to work but the results aren’t coming out properly. So far I’m just testing the addition member function to make sure its working but it is just outputting the first entered values. Below is my code:

#include <iostream>
#include <cmath>
#include <cstdlib>
#include <fstream>
#include <iomanip>

using namespace std;

class Rational
{
    private:
        int numer;
        int denomer;

    public:
        Rational()
        {
            numer = 0;
            denomer = 1;
        }

        int getnumer()
        {
            return numer;
        }

        void setnumer(int numer)
        {
            numer = numer;
        }

        int getdenomer ()
        {
            return denomer;
        }

        void setdenomer (int denomer)
        {
            denomer = denomer;
        }

        Rational(int n, int d)
        {
            numer = n;
            denomer = d;
        }

        Rational (int n)
        {
            numer = n;
            denomer = 1;
        }

        Rational add (Rational r1)
        {
            int a, b, c, d, r;
            Rational r2;

            a = getnumer();
            b = getdenomer();
            c = r1.getnumer();
            d = r1.getdenomer();

            r2.setnumer(a * d + b * c);
            r2.setdenomer(b * d);

            return r2;
        }

        Rational sub (Rational r1, Rational r2)
        {
            int a, b, c, d, r;

            a = getnumer();
            b = getdenomer();
            c = r1.getnumer();
            d = r1.getdenomer();

            r2.setnumer(a * d - b * c);
            r2.setdenomer(b * d);

            return r2;
        }

        Rational mul (Rational r1, Rational r2)
        {
            int a, b, c, d, r;

            a = getnumer();
            b = getdenomer();
            c = r1.getnumer();
            d = r1.getdenomer();

            r2.setnumer(a * c);
            r2.setdenomer(b * d);

            return r2;
        }

        Rational div (Rational r1, Rational r2)
        {
            int a, b, c, d, r;

            a = getnumer();
            b = getdenomer();
            c = r1.getnumer();
            d = r1.getdenomer();

            r2.setnumer(a * d);
            r2.setdenomer(c * b);

            return r2;
        }

        bool lesst (Rational r1)
        {
            int a, b, c, d, r;

            a = getnumer();
            b = getdenomer();
            c = r1.getnumer();
            d = r1.getdenomer();

            if ((a * b) < (c*b))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        Rational neg (Rational r1, Rational r2)
        {
            int a, b, c, d, r;

            a = getnumer();
            b = getdenomer();
            c = r1.getnumer();
            d = r1.getdenomer();

            r2.setnumer(-a);
            r2.setdenomer(-b);

            return r2;
        }

        Rational equal_num (Rational r1, Rational r2)
        {
            int a, b, c, d, r;

            a = getnumer();
            b = getdenomer();
            c = r1.getnumer();
            d = r1.getdenomer();

            if ((a * d) == (c * b))
            {
                cout << "The numbers are equal";
            }
            else
            {
                // Nothing
            }
        }

        void printNumber()
        {
            cout << getnumer() << "/" << getdenomer();
        }


        //void inputz (istream& fin)
};

int main()
{
    int num1, den1, num2, den2;

    cout << "Please enter the first numerator value: ";
    cin >> num1;

    cout << "Please enter the first denominator value: ";
    cin >> den1;

    cout << "Please enter the second numerator value: ";
    cin >> num2;

    cout << "Please enter the second denominator value: ";
    cin >> den2;

    cout << "nAddition: ";
    cout << num1 << " / " << den1 << " + " << num2 << " / " << den2 << " = ";
    Rational r1(num1, den1);
    Rational r2 (num2, den2);

    r1.add(r2);

    r1.printNumber();

    return 0;
}

And the output I get is:

Please enter the first numerator value: 2
Please enter the first denominator value: 3
Please enter the second numerator value: 1
Please enter the second denominator value: 2

Addition: 2 / 3 + 1 / 2 = 2/3

Source: Windows Questions C++

LEAVE A COMMENT