Rational numbers help

Hi I have this programming assignment and I have no idea what to do. I thought I did but apparently I don't.

I'm supposed to make a class Rational
and I'm supposed to be able to do fractions, not decimals.
for example I need the input to look like this 1/2
and the output to look like 1/2 as well.

There's more but can anyone help me get started on this part?

"Overload the input and output operators >> and <<. Numbers are to be input and output in the form 1/2,
15/32, 300/401, and so forth."
Also what does that mean? How do I even do tat?


Well I solved the fraction part although I have no idea what the rest of the program is supposed to do (or I think I solved it with cin.ignore(1))
Last edited on
You haven't thoroughly explained your problem here. Please explain more about what the class will actually process.
"Define a class for rational numbers. A rational number is a number that can be represented as the quotient of
two integers. For example, 1/2, 3/4, 64/2, and so forth are all rational numbers. (By 1/2 and so on we mean the
everyday fraction, not the integer division this expression would produce in a C++ program.)
Represent rational numbers as two values of type int, one for the numerator and one for the denominator.
Call the class Rational.
Include a constructor with two arguments that can be used to set the member variables of an object to
any legitimate values.
Also include a constructor that has only a single parameter of type int; call this single parameter
wholeNumber and define the constructor so that the object will be initialized to the rational number
wholeNumber/1.
Include a default constructor that initializes an object to 0 (that is, to 0/1). "
Verbatim
As far as I can see, there is nothing to do except overload and input fractions/rational numbers.
Other than that I don't know what the point of the assignment is other than fractions

"Overload the input and output operators >> and <<. Numbers are to be input and output in the form 1/2,
15/32, 300/401, and so forth. Note that the numerator, the denominator, or both may contain a minus sign, so -
1/2, 15/-32, and -300/-401 are also possible inputs.
Overload all the following operators so that they correctly apply to the type Rational: ==, +, -, *, and /.


Declare a predefined class vector object (please read the book for details about the class vector) and
add these 5 rational numbers to the vector object. Access the elements in the vector and print them.
Then, delete the element in the middle of the vector. At this point, you need to figure out how you can
delete an element from a vector? Then, please print the values again to see the change in the vector. "

This is all I have so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <vector>
 using namespace std;

 class Rational{
 
 public:
	 int x, y;
	 int fraction(int, int);
	 int wholeNumber(int);
 private:
 
 
 };

 int main()
 {
Rational rat;
	 cout<<"Enter your number: "<<endl;
	 cin>>rat.x;
	 cin.ignore(1);
	 cin>>rat.y;
		 rat.y;
	 cout<<rat.x<<"/"<<rat.y;
 

 system("Pause");
 return 0;
 }
Last edited on
Your professor should have shown you what the extraction and insertion operators look like, and how to overload them. For your class, they should look like:

1
2
3
4
5
6
7
8
9
10
11
istream& operator >> ( istream& ins, Rational& r )
{
        // read a rational number from ins (not from cin) here
        return ins;
}

ostream& operator << ( ostream& outs, const Rational& r )
{
        // write a rational number to outs (not to cout) here
        return outs;
}

Now your main function will look like:

1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
        Rational rat;

        cout << "Enter your number: ";
        cin >> rat;

        cout << "Good job! You entered the number " << rat << ".\n";

        // Please read  http://www.cplusplus.com/articles/iw6AC542/
        return 0;
}

Hope this helps.
May I ask why you use
1
2
3
4
5
6
7
8
9
10
11
istream& operator >> ( istream& ins, Rational& r )
{
        // read a rational number from ins (not from cin) here
        return ins;
}

ostream& operator << ( ostream& outs, const Rational& r )
{
        // write a rational number to outs (not to cout) here
        return outs;
}
?

Or is that overloading? and am I supposed to read from a file or something?
Nevermind I put them both in public class to initialize but they both had an error saying too many parameters so I just made them friends
1
2
friend ostream& operator << (ostream& outputStream, const Rational& r );
	 friend istream& operator >> (istream& inputStream, Rational& r);
Last edited on
That's right: make them friends.

You have no idea whether you are reading from or writing to a file, the standard I/O, a pipe, etc. That is part of the whole point. All you know is you have a reference to a stream which you can use to read/write information.

Hope this helps.
So how am I supposed to get the input?
I input them myself or I input them all at the same time or what?
I'm really confused
because I forgot to mention this part of the directions:

"Write a test program to test your class and overloaded operators. Read the following 5 rational numbers using
the overloaded input operator.
1/2
1/-2
-1
0
5/2
1) Test all the overload operators and make sure that they work properly. For example:
If( (ratNum1/ratNum2) == (ratNum3)
cout << ratNum1+ratNum4;
else
cout << ratNum1-ratNum5; "

Does that mean I read all 5 consecutively?
like inputStream>> etcetc;?
Er, let me clarify.

When you are coding the overloaded I/O operators, you have no idea where the input is coming from or where the output is going to.

When you are using them, then you specify what stream to use, whether it be cin or a file or whatever.

So, inside your input operator, you will read from inputStream, and inside your output operator, you will write to outputStream.

In your main() function, you will use cin >> rat; and cout << rat << endl;, etc.

Hope this helps.
Oh that's what you meant. I know that (;

But like I've never used inputStream or outputStream in anything other than files so I don't know how to use it other than reading from a file...
Could you maybe, link me to somewhere that explains better? (that's why I didn't know what you were talking about)
I know it's basically the same thing as cout<< and cin>> except different named and it doesn't output onto the screen. But for inputStream, I always read from file so I never had to read anything except a variable....

x_o

Because like you said here in the comments
1
2
3
4
5
6
7
8
9
10
11
istream& operator >> ( istream& ins, Rational& r )
{
        // read a rational number from ins (not from cin) here
        return ins;
}

ostream& operator << ( ostream& outs, const Rational& r )
{
        // write a rational number to outs (not to cout) here
        return outs;
}

I read the number from there which means I type it from there
such as in your example ins>> 2/3?
Last edited on
No, when you call cin >> rat you are calling the overloaded >> in your class. ins>>var would be internal to the overloaded stream operator.

EDIT: You need to code your stream operators to accept 1/2 as input ie:cin >> rat stores the input 1/2, and cout << rat prints the fraction (1/2).
Last edited on
Thank you, I will try to figure that out.

Am I supposed to initialize the functions IN the class? Or does that supposed to be outside of class and main function? I can't remember but I tried initilization a default constructor but it wouldn't work so I just started initializing everything inside the class... Is that okay?

And fml , no I don't understand those overloaded things ;-;.
How would I input or output anything if I can't SEE it?
isn't everything internal so how would I see if anything is happening?
cout?
Last edited on
@naraku
No?! YES

@Tatipu
He misunderstood your question.

Inside the operator >> function, you do read from ins.

Taking what you wrote in your original main():

18
19
20
21
22
Rational rat;
	 cout<<"Enter your number: "<<endl;
	 cin>>rat.x;
	 cin.ignore(1);
	 cin>>rat.y;

...the part that reads the Rational number from cin is on lines 20 through 22.

We'll move that to the operator >> function, and change 'cin' to 'inputStream':

1
2
3
4
5
6
7
8
istream& operator >> ( istream& inputStream, Rational& r )
{
	 inputStream>>r.x;
	 inputStream.ignore(1);
	 inputStream>>r.y;

	 return inputStream;  // (don't forget this part)
}

(I name my input streams "ins", but this uses your name for it: "inputStream". Whatever name you give it is not so important -- that is, you can name the argument as you like.)

Now you can change your original main to read thusly:

18
19
20
21
22
Rational rat;
	 cout<<"Enter your number: "<<endl;
	 cin>>rat;


That's because line 20 now uses your fancy overloaded operator >> function.

You need to do the same sort of thing for the operator << function.


BTW, they are called the "extraction operator" ('extracting' information from a stream, or 'input') and the "insertion operator" ('inserting' information into a stream, or 'output').

Hope this helps.
Last edited on
Topic archived. No new replies allowed.