I'm lost based on the code provided, how to reduce a fraction down. I've looked on the internet and yes most are using global variables; I however, can not for my class. It must be within the function. And there needs to be two calls outside of each of the operators to the reduce() function. Where am I going wrong?

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

// the setNumerator function sets the numerator value for the object.

void Rational::setNumerator (int numer)
{
    numeratorValue = numer;
}

// setDenominator is responsible for changing the denominatorValue data
// member.  It does some basic error checking to make sure we don.t try
// to set a 0 as the denominator. Our solution to this is to set the
// denom to 1 after outputting an error message

void Rational::setDenominator (int denom)
{

    if (denom != 0)
    {
        denominatorValue = denom;
    }
    else
    {
        cerr << "Illegal denominator.  Using 1 instead" << endl;
        denominatorValue = 1;
    }

    return;
}

// specific constructor, called when one or two arguments are given
// in object instantiation.  Sets rational to appropriate value

Rational::Rational (int numer, int denom)
{
    setNumerator(numer);
    setDenominator(denom);
    reduce();
}

// getNumerator and getDenominator are simple, private inspector
// member functions that return the values of the numerator and
// denominator for our object.

int Rational::getNumerator() const
{
    return numeratorValue;
}


int Rational::getDenominator() const
{
    return denominatorValue;
}

// add() performs addition of two Rational objects.  One object
// is doing the addition, the other object (the argument) is being
// added.  This function creates a very (very!) temporary third
// Rational object as it.s setting itself up to return a value.  It
// returns the newly created Rational object.  This is one of
// the few times you see a constructor called explicitly.

Rational Rational::add (const Rational &r) const
{
    int a = getNumerator();
    int b = getDenominator();
    int c = r.getNumerator();
    int d = r.getDenominator();
    Rational result(a*d + b*c, b*d);
    result.reduce();
    return result;
}

// multiply() performs multiplying two Rational objects.  One object
// is doing the multiplying, the other object (the argument) is being
// multiplied.  This function creates a very (very!) temporary third
// Rational object as it.s setting itself up to return a value.  It
// returns the newly created Rational object.  This is one of
// the few times you see a constructor called explicitly.

Rational Rational::multiply (const Rational &r) const
{
    int a = getNumerator();
    int b = getDenominator();
    int c = r.getNumerator();
    int d = r.getDenominator();
    Rational result(a*c, b*d);
    result.reduce();
    return result;
}

// insert() takes an ostream as the only argument.  An ostream
// is an output stream, like cout.  by making this an argument, you
// could output Rational numbers to files or other devices or streams.

void Rational::insert (ostream &streamout) const
{
    streamout << getNumerator() << "/" << getDenominator();
    return;
}

// extract() takes an istream as the only argument.  An istream
// is an input stream, like cin.  by making this an argument, you
// could input Rational numbers from files or other devices or
// streams.

void Rational::extract (istream &streamin)
{
    int numer;
    int denom;
    char slash;
    streamin >> numer >> slash >> denom;
    setNumerator(numer);
    setDenominator(denom);
    return;
}

// Here we implement the + operator.  all this "function" does is end
// up calling the add() behavior of the rational number class.
// by overloading the operator, we are providing the .natural
// interface. for the object that we are looking for to make working
// with Rational objects intuitive.

Rational operator+ (const Rational &left, const Rational &right)
{
    return left.add(right);
}

// and here is the implementation for the * operator.  We simply end up
// calling the multiply() behavior of a rational object.

Rational operator* (const Rational &left, const Rational &right)
{
    return left.multiply(right);
}

// here is the implementation for the << operator.  We simply end
// up calling the insert() behavior of a rational object.  Because
// of the "nature of output" in C++ (to be explained in more detail in 1620)
// we need to return the ostream out of the function.  This is
// a critical step in overloading the << operator.

ostream& operator<< (ostream &output, const Rational &rat)
{
    rat.insert(output);
    return output;
}

istream& operator>> (istream &input, Rational &rat)
{
    rat.extract(input);
    return input;
}



Rational Rational::subtract (const Rational &r) const
{
    int a = getNumerator();
    int b = getDenominator();
    int c = r.getNumerator();
    int d = r.getDenominator();
    Rational result(a*d - b*c, b*d);
    result.reduce();
    return result;
}

Rational operator- (const Rational &left, const Rational &right)
{
    return left.subtract(right);
}

Rational Rational::divide (const Rational &r) const
{
    int a = getNumerator();
    int b = getDenominator();
    int c = r.getNumerator();
    int d = r.getDenominator();
    Rational result(a*d, b*c);
    result.reduce();
    return result;
}

Rational operator/ (const Rational &left, const Rational &right)
{
        return left.divide(right);
}


bool Rational::lessThan (const Rational &r) const
{
    int a = getNumerator();
    int b = getDenominator();
    int c = r.getNumerator();
    int d = r.getDenominator();
    if (a*d < c*b)
        return true;
    else
        return false;

}

bool Rational::lessThanEqual (const Rational &r) const
{
    int a = getNumerator();
    int b = getDenominator();
    int c = r.getNumerator();
    int d = r.getDenominator();
    if ( a*d <= c*b)
        return true;
    else
        return false;
}

bool Rational::greaterThan (const Rational &r) const
{
    int a = getNumerator();
    int b = getDenominator();
    int c = r.getNumerator();
    int d = r.getDenominator();
    if (a*d > c*b)
        return true;
    else
        return false;
}

bool Rational::greaterThanEqual (const Rational &r) const
{
    int a = getNumerator();
    int b = getDenominator();
    int c = r.getNumerator();
    int d = r.getDenominator();
    if (a*d >= c*b)
        return true;
    else
        return false;
}

bool Rational::equalTo (const Rational &r) const
{
    int a = getNumerator();
    int b = getDenominator();
    int c = r.getNumerator();
    int d = r.getDenominator();
    if ((a==c) && (b==d))
        return true;
    else
        return false;
}

bool Rational::notEqualTo (const Rational &r) const
{
    int a = getNumerator();
    int b = getDenominator();
    int c = r.getNumerator();
    int d = r.getDenominator();
    if ((a!=c) || (b!=d))
        return true;
    else
        return false;
}
void Rational::reduce()
{
    int x = getNumerator();
    int a;
    x = a;
    int y = getDenominator();
    int b;
    y = b;



    int i;
    while (i = (a % b))
    {
        a = b;
        b = i;
    }
    x /= b;
    y /= b;

}

bool operator< (const Rational &left, const Rational &right)
{
    return left.lessThan(right);
}

bool operator<= (const Rational &left, const Rational &right)
{
    return left.lessThanEqual(right);
}

bool operator> (const Rational &left, const Rational &right)
{
    return left.greaterThan(right);
}

bool operator>= (const Rational &left, const Rational &right)
{
    return left.greaterThanEqual(right);
}

bool operator== (const Rational &left, const Rational &right)
{
    return left.equalTo(right);
}

bool operator!= (const Rational &left, const Rational &right)
{
    return left.notEqualTo(right);
}
void Rational::reduce()
{
    int x = getNumerator();
    int a;
    x = a;

What is the value of x supposed to be right now?

int y = getDenominator();
    int b;
    y = b;

What is the value of y supposed to be right now?

int i;
    while (i = (a % b))

Why are you doing arithmetic with a and b, if they are uninitialised?

{
        a = b;
        b = i;
    }
    x /= b;
    y /= b;
}

How are the numerator and denominator supposed to change if you don't actually set them to the newly calculated values?

I just wrote this for my class:

//written by Ben Hinrichs 2009
#include <iostream.h>

class RationalNumber
{
public:
	RationalNumber()
	{
		doTheMath();	
	}
	
void doTheMath()
	{
		cout << "Enter an int: ";
		cin >> a;
		cout << "Enter a denominator: ";
		cin >> b;
		if (b <= 0)
			cout << "Enter a valid denominator\n";
		else
		{
			c = a;
			d = b;

			cout << c << "/" << d << " reduces to ";

			if(b % a == 0)
			{
				gcf = a;
				c = a / gcf;
				d = b / gcf;
				cout << c << "/" << d << endl;
			}
			else
			{
				gcf = 0;
				do
				{
					modul = b % a;
					gcf = a;
					b = a;
					a = modul;
				} while(modul != 0);
				c = c / gcf;
				d = d / gcf;
				cout << c << "/" << d << endl;
			}
			
		}
	}
private:
        int a, b, c, d, modul, gcf;
};



int main()
{	
	RationalNumber Ponies;

	return 0;
}
commented: And you bumped a three year old thread to let us know that? -3

Changed Xanatox's code a litle bit to make the output a little easier. In case the denominator is 1, -1, the same as the numerator, or the opposite of the numerator. Credit for the original all goes to him.

void reduce(int a, int b){
     double modul=0, gcf=0, c=0, d=0;
       
			c = a;
			d = b;
           
			cout << c << "/" << d << " reduces to ";
            
            if(c==0){
            cout<<0<<endl;
            return;}
            
			if(b % a == 0)
			{
				gcf = a;
				c = a / gcf;
				d = b / gcf;
			    if(d==c)
			    cout<<c<<endl;
			    else if(d==c+(-2*c))
			    cout<<-1<<endl;
			    else if(d==1)
			    cout<<c<<endl;
			    else if(d==-1)
			    cout<<c+(-2*c)<<endl;
            }
			else
			{
				gcf = 0;
				do
				{
					modul = b % a;
					gcf = a;
					b = a;
					a = modul;
				} while(modul != 0);
				c = c / gcf;
				d = d / gcf;
				if(d==c)
			    cout<<1<<endl;
			    else if(d==c+(-2*c))
			    cout<<-1<<endl;
			    else if(d==1)
			    cout<<c<<endl;
			    else if(d==-1)
			    cout<<c+(-2*c)<<endl;
			}
	cout<<endl;
}

just realized my previous post needs:

else
cout<<c<<"/"<<d<<endl;

at the end of both else if statements.

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.