I have created 3 separate files but having issues with rationalnumber.cpp saying 'Rational' is not a class or namespace name. Any suggestions?

Below is what should happen.....but I'm stuck

thanks

the RationalNumber class should overload the stream insertion (<<) and stream extraction (>>) operators. The stream extraction operator should prevent a 0 denominator in a fraction, reduce fractions that are not in reduced form, and prevent negative denominators. Negative or 0 denominators should be set to 1.

Sample Output 1 (user input is in red):

Enter a Rational Number (n/d): 1/3
Enter a Rational Number (n/d): 2/4
1/3 + 1/2 = 5/6
1/3 - 1/2 = -1/6
1/3 * 1/2 = 1/6
1/3 / 1/2 = 2/3
1/3 <= 1/2 according to the overloaded > operator
1/3 < 1/2 according to the overloaded >= operator
1/3 < 1/2 according to the overloaded < operator
1/3 <= 1/2 according to the overloaded <= operator
1/3 != 1/2 according to the overloaded == operator
1/3 != 1/2 according to the overloaded != operator

Sample Output 2 (user input is red):

Enter a Rational Number (n/d): 2/2
Enter a Rational Number (n/d): 1/0
Invalid denominator: 0
1/1 + 1/1 = 2/1
1/1 - 1/1 = 0/1
1/1 * 1/1 = 1/1
1/1 / 1/1 = 1/1
1/1 <= 1/1 according to the overloaded > operator
1/1 >= 1/1 according to the overloaded >= operator
1/1 >= 1/1 according to the overloaded < operator
1/1 <= 1/1 according to the overloaded <= operator
1/1 == 1/1 according to the overloaded == operator
1/1 == 1/1 according to the overloaded != operator


driver.cpp

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


int main()
{
    // RationalNumber c( 1, 3 ), d( 2, 4 ), x;
    RationalNumber c, d, x;

    // test overloaded stream extraction operator
    cout << "Enter a Rational Number (n/d): ";
    cin >> c;
    cout << "Enter a Rational Number (n/d): ";
    cin >> d;

  //  x = c + d; // test overloaded operators + and =
    cout << c << " + " << d << " = " << x << endl;

//    x = c - d; // test overloaded operators - and =
    cout << c << " - " << d << " = " << x << endl;

//    x = c * d; // test overloaded operators * and =
    cout << c << " * " << d << " = " << x << endl;

//    x = c / d; // test overloaded operators / and =
    cout << c << " / " << d << " = " << x << endl;

    // test overloaded > operator
    cout << c << ( ( c > d ) ? " > " : " <= " ) << d
         << " according to the overloaded > operator\n";

    // test overloaded >= operator
    cout << c << ( ( c >= d ) ? " >= " : " < " ) << d
         << " according to the overloaded >= operator\n";

    // test overloaded < operator
    cout << c << ( ( c < d ) ? " < " : " >= " ) << d
         << " according to the overloaded < operator\n";

    // test overloaded <= operator
    cout << c << ( ( c <= d ) ? " <= " : " > " ) << d
         << " according to the overloaded <= operator\n";

    // test overloaded == operator
    cout << c << ( ( c == d ) ? " == " : " != " ) << d
         << " according to the overloaded == operator\n";

    // test overloaded != operator
    cout << c << ( ( c != d ) ? " != " : " == " ) << d
         << " according to the overloaded != operator\n";

    return 0;
} // end main

RationalNumber.cpp

#include <iostream>
#include <iomanip>
//using std::cout;
//using std::istream;
//using std::ostream;
//using std::endl;
using namespace std;

#include "RationalNumber.h"

Rational::Rational(int numerator, int denominator)
{
	this->numerator = numerator;
	//this->denominator = denominator;
	if(denominator ==0)
	{
		this->denominator = 1;
	}// end if
	reduction();
}

	bool RationalNumber::operator>(RationalNumber &number)
	{
		return (double) numerator / denominator > (double) number.numerator / number.denominator;
	}
	bool RationalNumber::operator>=(RationalNumber &number)
	{
		return (double) numerator / denominator >= (double) number.numerator / number.denominator;
	}
	bool RationalNumber::operator<(RationalNumber &number)
	{
		return (double) numerator / denominator < (double) number.numerator / number.denominator;
	}
	bool RationalNumber::operator<=(RationalNumber &number)
	{
		return (double) numerator / denominator <= (double) number.numerator / number.denominator;
	}
	bool RationalNumber::operator==(RationalNumber &number)
	{
		return (double) numerator / denominator == (double) number.numerator / number.denominator;
	}
	bool RationalNumber::operator!=(RationalNumber &number)
	{
		return (double) numerator / denominator != (double) number.numerator / number.denominator;
	}
	RationalNumber &RationalNumber::operator=(RationalNumber &number)
	{
		numerator = number.numerator;
		denominator = number.denominator;
		return *this;
	}
	istream &operator>>(istream &in, RationalNumber &number)
	{
		char slash;
		in >> number.numerator >> slash >> number.denominator;
		if(number.denominator == 0)
		{
			cout<<"invalid denominator/n";
			number.denominator = 1;
		}
		return &in;
	}
	ostream &operator<<(ostream &out, RationalNumber &number)
	{
		out << number.numerator << '/' << number.denominator;
		
		return &out;
	}

void Rational::printRational()
{
	cout << numerator << "/"<< denominator;
}//end printRational

void Rational::printRationalAsDouble()
{
	cout << (double) numerator / denominator;
}// end printRationalAsDouble

void Rational::reduction()
{
   int largest; 
   largest = numerator > denominator ? numerator : denominator;

   int gcd = 0; // greatest common divisor

   for ( int loop = 2; loop <= largest; loop++ )

       if ( numerator % loop == 0 && denominator % loop == 0 )
          gcd = loop;

   if (gcd != 0) 
   {
      numerator /= gcd;
      denominator /= gcd;
   } // end if 
} // end function reduction

Rational::Rational()
{
	numerator = 0;
	denominator = 1;
}//end rational()

RationalNumber.h

#ifndef RATIONALNUMBER_H
#define RATIONALNUMBER_H

#include <iostream>
//using std::istream;
//using std::ostream;

//class
class RationalNumber
{
	friend istream &operator>>(istream&, RationalNumber&);
	friend ostream &operator<<(ostream&, RationalNumber&);
public:
	RationalNumber(int,int);
	RationalNumber();
	void printRationalNumber();
	RationalNumber &operator=(RationalNumber&);
	bool operator>(RationalNumber&);
	bool operator>=(RationalNumber&);
	bool operator<(RationalNumber&);
	bool operator<=(RationalNumber&);
	bool operator==(RationalNumber&);
	bool operator!=(RationalNumber&);
	void printRationalNumberAsDouble();
	
	void reduction();

private:
	int numerator;
	int denominator;
};
#endif

trying a different approach now...but its not quite working either.

#include <iostream>
#include <iomanip>
using namespace std;
#ifndef Rational

class Rational 
{
 public:
 Rational( int = 0, int = 1 ); // default constructor
 Rational addition( const Rational &);
 Rational subtraction( const Rational &);
 Rational multiplication( const Rational &);
 Rational division( Rational &);
 void printRational();
 void printRationalAsDouble();
 
 private:
 int numerator;
 int denominator;
 void reduction(); // utility function
 };

 #endif

 Rational::Rational( int n, int d )// constructor initialized with the num and den
 {
 numerator = n;
 denominator = d;
 }

 void Rational::printRational()
 {
 if ( denominator == 0 )//figure if the denominator is 0
 {
	cout << "\nError divided by zero!!!" << '\n';
 }
 else if( numerator == 0 )
 cout << 0;
 else
 cout << numerator << '/' << denominator;
 }

 void Rational::reduction()//reduces the fraction
 {
 int largest;
 largest = numerator > denominator ? numerator : denominator;

 int gcd = 0; // greatest common divisor

for ( int loop = 2; loop <= largest; loop++ )
 if ( numerator % loop == 0 && denominator % loop == 0 )
 gcd = loop;

 if (gcd != 0) 
 {
 numerator /= gcd;
 denominator /= gcd;
 }
 }

 Rational Rational::addition( const Rational &a )//add the fractions
 {
 Rational t;

          t.numerator = a.numerator * denominator;
          t.numerator += a.denominator * numerator;
          t.denominator = a.denominator * denominator;
          t.reduction();

 return t;
 }

 Rational Rational::subtraction( const Rational &s )//subtract the fractions
{
 Rational t;
          t.numerator = s.denominator * numerator;
          t.numerator -= denominator * s.numerator;
          t.denominator = s.denominator * denominator;
          t.reduction();

 return t;
 }

 Rational Rational::multiplication( const Rational &m )//multiplys th fractions
 {
 Rational t;
          t.numerator = m.numerator * numerator;
          t.denominator = m.denominator * denominator;
          t.reduction();

 return t;
 }

 Rational Rational::division( Rational &h )//divides the fractions
 {
 Rational t;
          t.numerator = h.denominator * numerator;
          t.denominator = denominator * h.numerator;
          t.reduction();

 return t;
 }
 int main()
{
	int c=0;
	int d=0;
	int x=0; 
	int RationalNumber;
    // RationalNumber c( 1, 3 ), d( 2, 4 ), x;
    RationalNumber= c, d, x;

    // test overloaded stream extraction operator
    cout << "Enter a Rational Number (n/d): ";
    cin >> c;
    cout << "Enter a Rational Number (n/d): ";
    cin >> d;

    x = c + d; // test overloaded operators + and =
    cout << c << " + " << d << " = " << x << endl;

    x = c - d; // test overloaded operators - and =
    cout << c << " - " << d << " = " << x << endl;

    x = c * d; // test overloaded operators * and =
    cout << c << " * " << d << " = " << x << endl;

    x = c / d; // test overloaded operators / and =
    cout << c << " / " << d << " = " << x << endl;

    // test overloaded > operator
    cout << c << ( ( c > d ) ? " > " : " <= " ) << d
         << " according to the overloaded > operator\n";

    // test overloaded >= operator
    cout << c << ( ( c >= d ) ? " >= " : " < " ) << d
         << " according to the overloaded >= operator\n";

    // test overloaded < operator
    cout << c << ( ( c < d ) ? " < " : " >= " ) << d
         << " according to the overloaded < operator\n";

    // test overloaded <= operator
    cout << c << ( ( c <= d ) ? " <= " : " > " ) << d
         << " according to the overloaded <= operator\n";

    // test overloaded == operator
    cout << c << ( ( c == d ) ? " == " : " != " ) << d
         << " according to the overloaded == operator\n";

    // test overloaded != operator
    cout << c << ( ( c != d ) ? " != " : " == " ) << d
         << " according to the overloaded != operator\n";

    system ("pause");
	return 0;
} // end main

Is the class named Rational or is the class named RationalNumber or are there two classes? If there is no class called Rational and you have anything that looks like Rational:: , the compiler/linker has no idea that it is supposed to be a class and will thus try to find a namespace called Rational . You have not provided one, so that's an error. Looks to me like there is not supposed to a Rational class. I would imagine that all references to Rational should be RationalNumber, so try doing a global replace changing "Rational" to "RationalNumber" (make sure the filenames are also "RationalNumber" too) and try cleaning and recompiling and see what happens.

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.