istream operator

Main function code:
1
2
3
4
5
Rational B;

cout << "Enter the value for Rational B: ";
cin >> B; //This is where I'm having trouble
cout << "The value of B is: " << B;

User Inputs: "50/1";
Desired Output: "The value of B is: 50;
Actual Output: "The Value of B is : 0; (0 is assigned to B from default constructor)

1
2
3
4
5
6
7
8
9
10
11
std::istream &operator>>(std::istream &in, Rational a){
	in >> a.equation;
	
	std::stringstream ss;
	ss << a.equation;
	ss >> a.numerator;
	ss.ignore();
	ss >> a.denominator;

	return in;
}


I've already written and understand the ostream operator for my class, but I haven't written my istream. What I have written of my istream isn't correct, I know that much, but I don't know why. Any advice would be appreciated!
Have the operator take Rational by reference.
Thanks!

fixed code:
1
2
3
4
5
6
7
8
9
10
11
std::istream &operator>>(std::istream &in, Rational &a){
	in >> a.equation;
	
	std::stringstream ss;
	ss << a.equation;
	ss >> a.numerator;
	ss.ignore();
	ss >> a.denominator;

	return in;
}
Topic archived. No new replies allowed.