## Tex_Waco_USMC

I am a fairly new programmer with limited knowledge in C++. I am currently writing a program for my class that involves fractions. I was doing good writing one piece at a time and making it work until the instructor threw a wrench into my plans and asked me to output in both fraction form (I.E: a/b) and numeric form (I.E: 111.23). This is a homework assignment and it is already late. I hate turning in late assignments but, I hate even worse not being able to complete the assignment. It will probably be the death of me someday. Please help me solve this problem. The following is what I have so far.

``````#include <iostream>
#include <iomanip>

using namespace ::std;	//allows user to keep from entering "std::" in front of every cin,cout, or endl

/**********************************************************************/
class rational																//Begin Rational Class
{
private:
int num;
int denom;
public:

rational::rational( int n = 0 , int d = 0 ) : num( n ), denom( d ) 			//Begin Constructor
{
for ( int x = 2; x < num; x++ )
{
if ( ((num%x) == 0 ) && ((denom%x) == 0) )			// Greatest Common Denominator, Used to Simplify fractions
{
num /= x;
denom /= x;
x--;
}
}
}																//end of Constructor

/**********************************************************************/

rational rational::operator+ ( const rational &X )						// Completes the addition of the fractions
{
int newnum = (X.denom * num) + (denom * X.num);
int newdenom = X.denom * denom;
return rational( newnum , newdenom );
}
/**********************************************************************/

rational rational::operator- ( const rational &X ) const				// Completes the subtraction of the fractions
{
int newnum = ( num * X.denom ) - ( X.num * denom );
int newdenom = X.denom * denom;
return rational( newnum , newdenom );
}
/**********************************************************************/

rational rational::operator* ( const rational &X ) const				// Completes the multiplication of the fractions
{
int newnum = X.num * num;
int newdenom = X.denom * denom;
return rational( newnum , newdenom );
}
/**********************************************************************/

rational rational::operator/ ( const rational &X ) const				// Completes the division of the fractions
{
int newnum = num * X.denom;
int newdenom = X.num * denom;
return rational( newnum , newdenom );
}

friend ostream &operator << ( ostream&, const rational& );
friend istream &operator >> ( istream&, rational& );
};																			// End of Rational Class

/**********************************************************************/

istream &operator >> ( istream &in, rational &X )						//Input, ignores the / of the fraction when input
{
in >> X.num;
in.ignore();
in >> X.denom;
return in;
}

/**********************************************************************/
ostream &operator << ( ostream &out , const rational &X )				//Output format a/b
{
//out << static_cast< double >( X.num ) / X.denom ;
out << X.num << "/" << X.denom;
return out;

}

/**********************************************************************/

int main()																	//Beginning of Main
{
rational ans, frac1, frac2;

cout << "Welcome to the Fraction Arthmetic Program!\n" << endl;
cout << "Enter your 1st fraction (EX: a/b): ";
cin  >> frac1;
cout << "Enter your 2nd fraction (EX: a/b): ";
cin  >> frac2;

ans = frac1 + frac2;
cout << "\nAddition of: "<< frac1 << " + " << frac2 << " = " << ans << endl;
ans = frac1 - frac2;
cout << "Subtraction of: "<< frac1 << " - " << frac2 << " = " << ans << endl;
ans = frac1 * frac2;
cout << "Multiplication of: "<< frac1 << " * " << frac2 << " = " << ans << endl;
ans = frac1 / frac2;
cout << "Division of: "<< frac1 << " / " << frac2 << " = " << ans << "\n" << endl;

return 0;
}																		//End of Main``````

If I have not properly posted my code please help me out on this too. I think this is the way it is supposed to go, but if I am wrong please tell me so that in future posts I can correct myself. I am not trying to offend anyone. Thank You in advance.

## vmanes 1,165

Is there a question? What problem are you having?

## Tex_Waco_USMC

The problem I am having is that I can't figure out in my current situation how to get it to print the answer's out in both fraction form(what it is currently doing) and in double form. I have tried several different ways, but I can only get it to do one or the other but not both. I apologize that I wasn't clear in my original post.

## vmanes 1,165

If the instructor wants both form from a single output operation, then simply output both in whichever order, like

``out << static_cast< double >( X.num ) / X.denom << "  "  << X.num << "/" << X.denom;``

If the instructor wants a means to select which version, then I think you need separate method for one form, the ostream for the other.

## Tex_Waco_USMC

Vmanes,

I had to change some of the wording of the output but that worked great. I have one more question. The output is reducing the fractions as it should if the fraction is positive. However, if the fraction is negative it doesn't reduce them down all the way. For instance I input 1/2 and 3/4. The subtraction line had -2/8 as the answer. What have I done wrong?

## Tex_Waco_USMC

Nevermind! I figured it out. The GCD loop was ill-written. I got it working now. This is how I solved it:

``````rational::rational( int n = 0 , int d = 0 ) : num( n ), denom( d )          //Begin Constructor
{
int big;
big = num > denom ? num : denom;

for ( int x = 1; x < big; x++ )
{
if ( num % x == 0 && denom % x == 0 )           // Greatest Common Denominator, Used to Simplify fractions
{
num /= x;
denom /= x;
}
}
}
``````

This worked great. I really appreciate the help with this one.

## vmanes 1,165

Your code has `for ( int x = 2; x < num; x++ )` What does this do if num is negative?

You could test the signs of both num and den, use only the absolute value of each for computing GCD, then assign back the appropriate sign to one or the other.