Tonis White
Tonis White

Reputation: 35

Cannot acquire correct fractional values in output

I believe I've corrected most of the program. So far it's error-free, but I'm receiving odd values during a certain portion of execution:

My output is:

0/2
3/4
Cannot divide by zero
Cannot divide by zero
0
0
Cannot divide by zero
____________________________________

I know that my calculations in my functions are correct, but I believe they are not receiving the saved values for the variables being passed to them.

Here is my class:

#include <iostream>
#include <string.h>
#include <fstream>
using namespace std;

class fraction {
private:
    long num, den;

public:
    fraction() {}
    fraction(int n, int d) {
        num = n;
        if (d == 0) {
            cout << "Careful there, Tim..." << endl;
            exit(0);
        } else
            den = d;
    }

    void setNum(long l_num) { num = l_num; }
    void setDen(long l_den) { den = l_den; }
    long getNum() { return num; }
    long getDen() { return den; }

    long add(fraction& a, fraction& b) {
        int n = (a.getNum() * b.getDen()) + (b.getNum() * a.getDen());
        int d = a.getNum() * b.getDen();
        num = n / gcd(n, d);
        den = d / gcd(n, d);
        return num;
        return den;
    }

    long sub(fraction& a, fraction& b) {
        int n = a.getNum() * b.getDen() - b.getNum() * a.getDen();
        int d = a.getNum() * b.getDen();
        num = n / gcd(n, d);
        den = d / gcd(n, d);
    }

    long mult(fraction& a, fraction& b) {
        int n = a.getNum() * b.getNum();
        int d = a.getDen() * b.getDen();
        num = n / gcd(n, d);
        den = d / gcd(n, d);
    }

    long div(fraction& a, fraction& b) {
        int n = a.getNum() * b.getDen();
        int d = a.getDen() * b.getNum();
        num = n / gcd(n, d);
        den = d / gcd(n, d);
    }

    long inc(fraction& a) {
        int n = a.getNum() + 1;
        int d = a.getDen() + 1;
    }

    long gcd(long n, long d) {
        int remainder;
        while (d != 0) {
            remainder = n % d;
            n = d;
            d = remainder;
        }
        return n;
    }
    void print() // Display method
    {
        if (den == 1)
            cout << num << endl;
        else if (den == 0)
            cout << "Cannot divide by zero" << endl;
        else
            cout << num << "/" << den << endl;
    }
};

My instructor gave us strict instructions to NOT modify his given int main(), but here it is:

int main() { // define seven instances of the class fraction
    fraction f1, f2, f3, f4, f5, f6, f7;
    // set values for the numerator and denominator to f1 and print
    // them
    f1.setDen(2L);
    f1.setNum(0L);
    f1.print();

    // set values for the numerator and denominator to f2 and print them
    f2.setDen(4L);
    f2.setNum(3L);
    f2.print();
    f3.add(f1, f2);
    f3.print();
    f4.sub(f1, f2);
    f4.print();
    f5.mult(f1, f2);
    f5.print();
    f6.div(f1, f2);
    f6.print();
    f7.inc(f1);
    f7.print();
}

I've already requested help on this program once before from SO, and I was approached with very nice people. I've e-mailed my professor twice more, and called his office. He has not answered // read my e-mails read cc on open and has not answered the voicemail I've left.

Upvotes: 0

Views: 109

Answers (1)

Radu Bogdan
Radu Bogdan

Reputation: 504

i think i found you problem, on your functions for add/sub when you calculated d,

int d = a.getNum() * b.getDen();

You made a mistake, for the D, you should actually do :

int d = a.getDen() * b.getDen();

From your add function you should remove the two returns

   long add(fraction& a, fraction& b) {
        int n = (a.getNum() * b.getDen()) + (b.getNum() * a.getDen());
        int d = a.getNum() * b.getDen();
        num = n / gcd(n, d);
        den = d / gcd(n, d);
        //return num;
        //return den;
    }

And on your inc function you forgot to set the num,den:

   long inc(fraction& a) {
        num = a.getNum() + 1;
        den = a.getDen() + 1;
    }

Check this out http://ideone.com/C7KgD3 to see the results

Upvotes: 2

Related Questions