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 << "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':

break;

case 'S':
case 's':

break;

case 'M':
case 'm':

break;

case 'D':
case 'd':

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::sub(fraction f2){

}*/

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

}*/

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

}*/

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

}

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  sub(int, int, int, int);
//fraction  mult(int, int, int, int);
//fraction  div(int, int, int, int);
void    print();

};
``````

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 …

To make add a method of class fraction define it something like this:

and call it something like this:

I suspect that this:

ignores everything to the right of = …

## 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;

// declare as const so not to modify the object's contents
{
//   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    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;

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 << "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':
total.gcd(total);
total.print(total);
break;

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

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

case 'D':
case 'd':
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;
}
``````

To make add a method of class fraction define it something like this:

and call it something like this:

I suspect that this:

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;