### Help with algorithm for extraction operator

I am trying to program a Cartesian class with a friend extraction operator function. When I try to compile it I can't because of a problem in the implementation section. Can anyone tell me what algorithms I need to use to fix this?

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748`` `````` #include #include #include using namespace std; class Cartesian { private: double x; double y; public: Cartesian( double= 0, double= 0); friend istream& operator>>(istream&, Cartesian&); friend ostream& operator<<(ostream&, const Cartesian&); }; istream& operator>>( istream& in, Cartesian& num) { double c, d; num.x= c; num.y= d; in >> c; in >> d; return in; } ostream& operator<<( ostream& out, const Cartesian& num) { cout << "(" << num.x << ", " << num.y << ")" << endl; return out; } int main() { Cartesian coord1, coord2; cin >> coord1; cin>> coord2; cout << coord1; cout << coord2; return 0; }``````
 `` `` ``Cartesian coord1, coord2;``

You are attempting the construction of two objects of type Cartesian with an undefined constructor.
Okay I fixed that, but now my program's output is
(6.95319e-310, 0)
(6.95319e-310, 0)
Why didn't it ask for the user to input values for the coordinates?
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354`` ``````#include #include #include using namespace std; class Cartesian { private: double x; double y; public: Cartesian( double= 0, double= 0); friend istream& operator>>(istream&, Cartesian&); friend ostream& operator<<(ostream&, const Cartesian&); }; Cartesian::Cartesian(double a, double b) { x=a; y=b; } istream& operator>>( istream& in, Cartesian& num) { double a, b; num.x= a; num.y= b; in >> a; in >> b; return in; } ostream& operator<<( ostream& out, const Cartesian& num) { cout << "(" << num.x << ", " << num.y << ")" << endl; return out; } int main() { Cartesian coord1, coord2; cin >> coord1; cin>> coord2; cout << coord1; cout << coord2; return 0; }``````
 Why didn't it ask for the user to input values for the coordinates?

Input should be requested by the executable.

The issue is the assignment of garbage values to the member variables of the argument. Throughout the execution of the function that overloads the extraction operator, only the variables declared within its compound statement are set to the input. In other words, the function does not have the desired affect on the given object. This can easily be solved with the replacement of the right operands of the extraction operator with num.x and num.y.

 ``12`` ``````in >> num.x; in >> num.y;``````

You've seemingly confused non-reference variables with reference variables. Know that they do differ.
Last edited on
Okay so I fixed that and changed some other parts of my code a bit, but now I get an error for the x=c and y=d portions of the declaration section saying x and y don't name types. What should I do?
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364`` ``````#include #include #include using namespace std; class Cartesian { private: double x; double y; public: Cartesian( double= 0, double= 0); friend istream& operator>>(istream&, Cartesian&); friend ostream& operator<<(ostream&, const Cartesian&); double c; double d; x=c; y=d; }; Cartesian::Cartesian(double a, double b) { x=a; y=b; } istream& operator>>( istream& in, Cartesian& num) { in >> num.x; in >> num.y; return in; } ostream& operator<<( ostream& out, const Cartesian& num) { cout << "(" << num.x << ", " << num.y << ")" << endl; return out; } int main() { Cartesian coord1, coord2; cout << "Please enter the first x-coordinate: "; cin >> coord1.c; cout << "Please enter the first y-coordinate: "; cin >> coord1.d; cout << "Please enter the second x-coordinate: "; cin >> coord2.c; cout << "Please enter the second y-coordinate: "; cin >> coord2.d; cout << coord1; cout << coord2; return 0; }``````
Last edited on
You don't need the
 ``1234`` ``````double c; double d; x=c; y=d;``````
part.

Also, main should probably look something more like
 ``12345678`` ``````int main() { double x1, y1, x2, y2; // Get input for x1, y1, x2, y2 Cartesian coord1(x1, y1), coord2(x2, y2); cout << coord1; cout << coord2; }``````
Okay so I followed your advice and it at least runs now, but it doesn't allow for user input of the coordinates. How can I fix this?
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455`` ``````#include #include #include using namespace std; class Cartesian { private: double x; double y; public: Cartesian( double= 0, double= 0); friend istream& operator>>(istream&, Cartesian&); friend ostream& operator<<(ostream&, const Cartesian&); } ; Cartesian::Cartesian(double a, double b) { x=a; y=b; } istream& operator>>( istream& in, Cartesian& num) { in >> num.x; in >> num.y; return in; } ostream& operator<<( ostream& out, const Cartesian& num) { cout << "(" << num.x << ", " << num.y << ")" << endl; return out; } int main() { double x1, x2, y1, y2; Cartesian coord1(x1, y1), coord2(x2, y2); cout << "Please enter the first coordinates: "; cin >> coord1; cout << "Please enter the second coordinates: "; cin >> coord2; cout << coord1; cout << coord2; return 0; }``````
Whoops, I didn't notice at first that you had `operator>>` defined.
In that case, you should just be able to do
 ``123456`` ``````int main() { Cartesian coord1, coord2; cout << "Please enter the first coordinates: "; cin >> coord1; // When inputting, input just the numbers, e.g. 5 6 (not (5,6)) // ... ``````
Okay so I made those changes, but it still doesn't ask for user input. What am I doing wrong?
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354`` ``````#include #include #include using namespace std; class Cartesian { private: double x; double y; public: Cartesian( double= 0, double= 0); friend istream& operator>>(istream&, Cartesian&); friend ostream& operator<<(ostream&, const Cartesian&); } ; Cartesian::Cartesian(double a, double b) { x=a; y=b; } istream& operator>>( istream& in, Cartesian& num) { in >> num.x; in >> num.y; return in; } ostream& operator<<( ostream& out, const Cartesian& num) { cout << "(" << num.x << ", " << num.y << ")" << endl; return out; } int main() { Cartesian coord1, coord2; cout << "Please enter the first coordinates in the form x y: "; cin >> coord1; cout << "Please enter the second coordinates in the form x y: "; cin >> coord2; cout << coord1; cout << coord2; return 0; }``````
What do you mean by "it doesn't ask for user input"?
Does it just completely skip the `cin >> coord1;` and `cin >> coord2;` lines?
Does the program pause without the text "Please enter the first coordinates in the form x y: " showing up?

If it's the latter, try adding a `cout << flush;` between lines 44 and 45, and also between lines 46 and 47.
Topic archived. No new replies allowed.