Hello All,

I've put together this program for a rational class and it runs, but the problem is, it doesn't do what it's supposed to do. Any ideas? Thanks in advance.

See ya on the flipside,

Desh2350

// This program will do arithmetic operations on two rational numbers entered from the keyboard.

#include <iostream>

using namespace std;

class Rational

{
public:
	Rational(int num = 0, int den = 1);
	double addition (Rational c);
	double subtraction (Rational c);
	double multiplication (Rational c);
	double division (Rational c);
	void printRational ();
	void printRationalAsDouble ();





private:

	int numerator;	// initializes numerator
	int denominator;	// initializes denominator
	double d;	// initializes d
	double x;	// initializes x 

private:

	double reduction ();
};

Rational::Rational(int num, int den) : numerator(num), denominator(den)
{
 
}
 
double Rational::addition(Rational c)
{
numerator = numerator + c.numerator; // adds numerator
denominator = denominator + c.denominator; // add denominator
return reduction();
}
 
 
double Rational::subtraction(Rational c)
{
numerator = numerator - c.numerator; // subtracts numerator
denominator = denominator - c.denominator; // subtracts denominator
return reduction();
}
 
double Rational::multiplication(Rational c)
{
numerator = numerator * c.numerator; // multiplies numerator
denominator = denominator * c.denominator; // multiplies denominator
return reduction();
}
 
double Rational::division(Rational c)
{
numerator = numerator * c.denominator; // divides numerator and denominator
denominator = denominator * c.numerator; // divdes denominator and numerator
return reduction();
}
 
void Rational::printRational()
{
	cout << numerator << "/" << denominator; // prints fraction
}
 
void Rational::printRationalAsDouble()
{
	cout << d / static_cast<float>( x ); // prints as a double number
}
 
double Rational::reduction()

{
   int largest; // initializes largest
   largest = numerator > denominator ? numerator : denominator;
 
   int gcd = 0; // greatest common divisor
 
   for ( int loop = 2; loop <= largest; loop++ ) // initializes loop to 2, if loop is less that or equal to the largest, then increment the loop counter.
 
       if ( numerator % loop == 0 && denominator % loop == 0 ) //if the loop's remainder equals 0 and the denominator's remainder equals 0,
          gcd = loop; // then the greatest common denominator is assigned the value of the loop.
 
   if (gcd != 0)  
   {					// if the greatest common denominator does not equal 0, the divide both the numerator and the denominator by the greatest common denominator.
      numerator /= gcd;
      denominator /= gcd;
   } // end if 
 
   return gcd;
} // end function reduction

int main()

{
	Rational c; // instantiates objects c of class Rational

	cout << "The result of the calculation is:";
	c.printRational();
	cout << "The result of the calculation is:";
	c.printRationalAsDouble();

	fflush(stdin);
	cin.get();
}

The best way to get help with problems like this is to explain what answer you expected to get and what answer you actually got, in addition to relevant code. I'm going to guess that the output when you ran the program was something like 0/1 and junk output of some type. If so then note that the constructor you've declared doesn't initialize d or x before you try to use them.

By the way why you are using d and x instead of deniminator and numerator when displaying the object as type double.

double Rational::division(Rational c)
{
numerator = numerator * c.denominator; // divides numerator and denominator
denominator = denominator * c.numerator; // divdes denominator and numerator
return reduction();
}

You are multiplying here instead of dividing..

You may want to look at the answers given here, as many (if not most) of the same issues were addressed there, for the exact same problem set.

Basically the program is just supposed to do basic arithmetic operations (addition, subtraction, multiplication, and division) using two fractions that are input by the user. I run it, the command prompt comes up, and somehow it prints out a '0' at the end of the cout string, even though there isn't one in the code. I did initialize the numerator and denominator to 0 and 1 respectively, but when I take those values out, I get a 'C2512, no appropriate default constructor' error. I know that I need to somehow call the member functions into the main function and tell it to do the fore-mentioned calculations, but I'm not sure how to.

Thanks in advance,

Desh2350

Actually, given that you are not initializing c , this is pretty much what you would expect to see, as the result of dividing 0 by 1.0 is in fact zero.

In order to get a different value for c , you'll want to initialize it when you declare it:

Rational c(3, 4);    // three-fourths

Or else, add setters to change the numerator and denominator after the fact.

Actually, you should be getting NaN (Not a Number), for the second part, as you never initialize d and x at all. Those two instance variables are redundant, in any case; you want to remove those two variables outright, and divide numerator by denominator :

cout << numerator / static_cast<double>( denominator ); // prints as a double number
// This program will do arithmetic operations on two rational numbers entered from the keyboard.

#include <iostream>

using namespace std;

class Rational
{
	public:
		Rational(int num = 0, int den = 1);
		Rational addition (Rational c);
		Rational subtraction (Rational c);
		Rational multiplication (Rational c);
		Rational division (Rational c);
		void printRational ();
		void printRationalAsDouble ();
		

	private:

		int numerator;	// initializes numerator
		int denominator;	// initializes denominator
		void reduction ();
};

Rational::Rational(int num, int den) : numerator(num), denominator(den)
{
 
}
 
Rational Rational::addition(Rational c) //  B.addition(c)
{
	Rational temp;
	temp.numerator = (numerator * c.denominator) + (c.numerator * denominator); // adds numerator
	temp.denominator = denominator * c.denominator; // add denominator
	temp.reduction();
	return temp;
	
}
 
 
Rational Rational::subtraction(Rational c)
{
	Rational temp;
	temp.numerator = (numerator * c.denominator) - (c.numerator * denominator); // adds numerator
	temp.denominator = denominator * c.denominator; // add denominator
	temp.reduction();
	return temp;
}
 
Rational Rational::multiplication(Rational c)
{
	Rational temp;
	temp.numerator = numerator * c.numerator; // multiplies numerator
	temp.denominator = denominator * c.denominator; // multiplies denominator
	temp.reduction();
	return temp;
}
 
Rational Rational::division(Rational c)
{
Rational temp;
temp.numerator = numerator * c.denominator; // divides numerator and denominator
temp.denominator = denominator * c.numerator; // divdes denominator and numerator
temp.reduction();
return temp;
}
 
void Rational::printRational()
{
	cout << numerator << "/" << denominator; // prints fraction
}
 
void Rational::printRationalAsDouble()
{
	cout << (double) numerator / denominator; // prints as a double number
}
 
void Rational::reduction()

{
   int largest; // initializes largest
   largest = numerator > denominator ? numerator : denominator;
 
   int gcd = 1; // greatest common divisor
 
   for ( int loop = 2; loop <= largest; loop++ ) // initializes loop to 2, if loop is less that or equal to the largest, then increment the loop counter.
 
       if ( numerator % loop == 0 && denominator % loop == 0 ) //if the loop's remainder equals 0 and the denominator's remainder equals 0,
          gcd = loop; // then the greatest common denominator is assigned the value of the loop.
 
   if (gcd != 0)  
   {					// if the greatest common denominator does not equal 0, the divide both the numerator and the denominator by the greatest common denominator.
      numerator /= gcd;
      denominator /= gcd;
   } // end if 
 
  
} // end function reduction

void main()

{

	Rational c, x, z; // instantiates objects c and x of class Rational
	
	cout << "Please Enter a Rational Number: ";
	cin >>
	
		

		c.printRational();
		cout << " + ";
		x.printRational();
		cout << " = ";
		z = c.addition(x);
		z.printRational();

		cout << endl;
		c.printRational();
		cout << " - ";
		x.printRational();
		cout << " = ";
		z = c.subtraction(x);
		z.printRational();
		
		cout << endl;
		c.printRational();
		cout << " * ";
		x.printRational();
		cout << " = ";
		z = c.multiplication(x);
		z.printRational();
		
	cout << endl;
		c.printRational();
		cout << " / ";
		x.printRational();
		cout << " = ";
		z = c.division(x);
		z.printRational();
		
		fflush(stdin);
		cin.get();
	
	

	

	
}

Thanks for the previous responses. At this point, all I need to do is make some adjustments to the code to allow the user to enter two rational numbers. Everything else is running just fine. My professor said something about overloading the operators, as the user will be entering two rational numbers. I've gone over the subject, but I'm not sure how to covert that to the actual changes that I need to make to the code. Any ideas? Thanks in advance.

See ya on the flipside,

Desh 2350

As an aside, why did you change your main() from int main() to void main() ? The correct form is int main() ; strictly speaking, void main() shouldn't compile at all.

Also, I would separate the class definition and implementation into separate files from the main() function, just to keep the whole thing cleaner. Note that you generally want to avoid using the using namespace std; directive in header and class implementation files, especially if you are going to implement an overloaded operator<< or operator>> for I/O.

Edited 5 Years Ago by Schol-R-LEA: n/a

Assuming we're talking about overloading the input and output functions, the general approach is to make a friend function which takes a stream of the appropriate type, and an object of the class to be printed. You would need to add the following declaration to your class:

friend std::ostream& operator<<(std::ostream& os, Rational& r);

You would then implement the function outside of the class, but because it is a friend , it would have access to the internals of the class. The output function is actually quite close to what you already have for you printRational() method:

std::ostream& operator<<(std::ostream& os, Rational& r)
{
    os << r.numerator << "/" << r.denominator;
    return os;
}

Note that I explicitly referred to the std namespace when declaring the ostream& objects; this is because (as explained here in mind-numbing detail) it defaults to assuming that the class ostream& is in the global namespace otherwise, wheras you actually want the class that is in the std namespace. This is why I had advised against the using directive earlier.

Overloading the istream operator>> is similar, but a bit more complicated, as you will want to read in the '/' but ignore it; using the ignore() method is probably the best way to do this.

Also, you need to be careful not to allow a zero denominator. Usually, you would throw an exception in this case, but I'm guessing your course hasn't covered exceptions yet. I'm not sure what to recommend, other than just making sure that the denominator is in a valid state when the function ends.

This article has been dead for over six months. Start a new discussion instead.