What I'm trying to so is have calc grab the add, mult, sub, div from the fraction.cpp and do that one thing I curious and can't figure out it the how to do the formula in the function and return the value to add the fractions. Can anyone give me an idea or somewhere to begin with that? I know the formulas like for addition its a/b + c/d = (ad + bc) / (b*d) I'm just not sure how to implement this any help is appreciated.

calc.cpp
#include <iostream>
#include "fraction.h"
using namespace std;


int main()
{
    fraction f;
    char    choice;
    int numerator;
    int denominator;

    do
    {
        cout << "A\tAdd\n";
        cout << "S\tSub\n";
        cout << "M\tMult\n";
        cout << "D\tDiv\n";
        cout << "E\tExit\n";

        cout << "\nChoice?: ";

        cin >> choice;
        cin.ignore();

        switch (choice)
        {
        case 'A':
        case 'a':
            f.read();

            break;

        case 'S':
        case 's':
            f.read();

            break;

        case 'M':
        case 'm':
            f.read();

            break;

        case 'D':
        case 'd':
            f.read();

            break;

        case 'e':
        case 'E':
            break;

        default :
            cerr << "Unrecognized choice: " <<
                choice << endl;
            break;
        }
    }
    while (choice != 'e' && choice != 'E');

    return 0;
}



fraction.cpp
#include <iostream>
#include "fraction.h"
using namespace std;

/*fraction fraction::add(){

}*/

/*fraction fraction::sub(fraction f2){


}*/

/*fraction fraction::mult(fraction f2){


}*/

/*fraction fraction::div(fraction f2){


}*/

void fraction::print()                              
{
    cout << numerator << "/" << denominator << endl;

}

void fraction::read(){

    cout << "Please enter a numerator: ";
    cin >> numerator;
    cout << "Please enter a denominator: ";
    cin >> denominator;
}


fraction.h
class fraction{
    private:
            int denominator;
            int numerator;

    public:
                    fraction() : numerator(0), denominator(1) {}
            //fraction  add();
            //fraction  sub(int, int, int, int);
            //fraction  mult(int, int, int, int);
            //fraction  div(int, int, int, int);
            void    print();
            void    read();


};

Recommended Answers

All 5 Replies

If you wanted to add two fractions together, you need two objects of class fraction. You should use a third object to store the result.

You already have the formula, so implementing is quite easy. Just remember, your fraction class represents only "one" fraction, so use more than one and do operations on them.

fraction a, b;
fraction c;

c = a.add(b);

// declare as const so not to modify the object's contents
fraction fraction::add(const fraction& right) const
{
    //   a/b + c/d
    // = (ad + cb) / (b*d)
    fraction result;
    int x, y;

    x = numerator * right.denominator;    // ad
    y = right.numerator * denominator;    // cb

    result.numerator = x + y;   // (ad + cb)
    result.denominator = denominator * right.denominator;   // (b*d)
    return result;    // return the addition of both fractions
}

Man I feel so confused haha, Will this way let me call the read function then the add function then the print function fomr the calc.cpp I want to make it basically a fraction calculator I just cant seem to get passed these dang functions : /

Okay so this is where I'm at now. I have everything entered in there but when I use the calc.cpp everytime I try to solve one of the problems I get 0/1 for every answers what am I doing wrong?

fraction.h
class fraction{

    private:
            int numerator;
            int denominator;

    public:
                    fraction() : numerator(0), denominator(1) {}
            void    read();
            void    add(fraction f1, fraction f2, fraction f3);  
            void    sub(fraction f1, fraction r2, fraction f3);  
            void    mult(fraction f1, fraction f2, fraction f3);  
            void    div(fraction f1, fraction f2, fraction f3);   
            void    gcd(fraction f3);
            void    print(fraction f3);

};



fraction.cpp
#include <iostream>
#include "fraction.h"
using namespace std;

void fraction::read(){

    char key;
    cout << "Please Enter Fraction (n/d): ";
    cin >> numerator >> key >> denominator;
    cout << "\n";
}

void fraction::add(fraction f1, fraction f2, fraction f3){

    f3.numerator = f1.numerator * f2.denominator + f1.denominator * f2.numerator;
    f3.denominator = f1.denominator * f2.denominator;
}

void fraction::sub(fraction f1, fraction f2, fraction f3){

    f3.numerator = f1.numerator * f2.denominator - f1.denominator * f2.numerator;
    f3.denominator = f1.denominator * f2.denominator;
} 

void fraction::mult(fraction f1, fraction f2, fraction f3){

    f3.numerator = f1.numerator * f2.numerator;
    f3.denominator = f1.denominator * f2.denominator;
}

void fraction::div(fraction f1, fraction f2, fraction f3){

    f3.numerator = f1.numerator * f2.denominator;
    f3.denominator = f1.denominator * f2.numerator;
}

void fraction::print(fraction f3){

    cout << "Answer = " << f3.numerator << "/" 
        << f3.denominator << "\n\n";

}

void fraction::gcd(fraction f3)                  // Simplify to lowest terms
{
    long tnum, tden, temp, gcd;

    tnum = labs(f3.numerator);
    tden = labs(f3.denominator);

    if(tden==0)
    { cout << "Illegal fraction: division by 0 \n"; exit(1); }
    else if(tnum==0)
    { f3.numerator=0; f3.denominator=1; return; }

    while(tnum != 0)
    {
        if(tnum < tden)
        { temp=tnum; tnum=tden; tden=temp; }
        tnum = tnum - tden;
    }
    gcd = tden;
    f3.numerator = f3.numerator / gcd;
    f3.denominator = f3.denominator / gcd;

}



calc.cpp
#include <iostream>
#include "fraction.h"
using namespace std;

int main()
{
    char    choice;
    fraction fraction1, fraction2, total;

    do
    {
        cout << "A\tAdd\n";
        cout << "S\tSub\n";
        cout << "M\tMult\n";
        cout << "D\tDiv\n";
        cout << "E\tExit\n";

        cout << "\nChoice?: ";

        cin >> choice;
        cin.ignore();

        switch (choice)
        {
        case 'A':
        case 'a':
            fraction1.read();
            fraction2.read();
            total.add(fraction1, fraction2, total);
            total.gcd(total);
            total.print(total);
            break;

        case 'S':
        case 's':
            fraction1.read();
            fraction2.read();
            total.sub(fraction1, fraction2, total);
            total.gcd(total);
            total.print(total);
            break;

        case 'M':
        case 'm':
            fraction1.read();
            fraction2.read();
            total.mult(fraction1, fraction2, total);
            total.gcd(total);
            total.print(total);
            break;

        case 'D':
        case 'd':
            fraction1.read();
            fraction2.read();
            total.div(fraction1, fraction2, total);
            total.gcd(total);
            total.print(total);
            break;

        case 'e':
        case 'E':
            break;

        default :
            cerr << "Unrecognized choice: " <<
                choice << endl;
            break;

        }
    }
    while (choice != 'e' && choice != 'E');

    return 0;
}

Your declaration and function call for add() are wrong.

To make add a method of class fraction define it something like this:
fraction add(fraction rhs);

and call it something like this:
total = fraction1.add(fraction2);

I suspect that this:
total = add(fraction1, fraction2, total);

ignores everything to the right of = and just gives you the default value of the variable called total on the left of = which is 0/1 by default (via the default constructor used to declare total).

1)
You should take a look at the Operators option in the C++ language.
That way you can just write the FRACTION3=FRACTION1+FRACTION2;
and not FRACTION3=FRACTION1.ADD(FRACTION2);
just an advice.

2)
Take a look at "THIS" , that way you can modify the class you are in
instead of sending it as a third variable.
(but again , using the operators you won't need this command
because you will just return a new fraction variable)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.