I am making a program with a Cartesian class. I want the user to be able to input 2 coordinates, but when I run it it doesn't ask for any values to be entered. It gives this output
Please enter the first coordinates: Please enter the second coordinates: (4.86129e-270, -1.97785e-41)
(4.86143e-270, -1.97785e-41)

Here is the code
Code:
```#include <iostream>
#include <istream>
#include <ostream>

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 >> b;

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: ";
cin >> coord1;
cout << "Please enter the second coordinates: ";
cin>> coord2;
cout << coord1;
cout << coord2;

return 0;
}```
Also, I want to add a memberwise assignment function to assign the values of coord1 to coord2. How would I go about doing so? 2. You're kidding right?

Look at lines 32-37. It creates two uninitialised variables a and b, copies their initial (undetermined) values into num.x and num.y [that explains the funny values you're seeing], reads value from the stream into a and b, and then (as the function returns) destroys the variables a and b. In that logic, there is no path by which the values read from the stream find their way into affecting values of num.x or num.y.

As an aside, given that the output stream operator outputs the coordinates in brackets and with a comma, usually the input stream operator is expected to cope with presence of bracket and comma in the stream (as well as their absence). 3. And what should I do as an alternative? 4. Assign the values of a and b to num after reading a and b from the stream
Kurt 5. Okay so I've changed my code around a bit, but now I get errors saying the x and y in x=c and y=d don't name types. What should I do?
Code:
```#include <iostream>#include <istream>
#include <ostream>

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;
}``` 6. Remove lines 17 through 20. Work out some proper mechanism of setting Cartesian's x and y members.

Stop doing things by guesswork. 7. I've gotten the program to run, but it doesn't ask the user to input data. Can anyone tell me what part of my code is wrong?
Code:
```#include <iostream>
#include <istream>
#include <ostream>

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;
}``` 8. I can't reproduce your problem. When I run the program, I can enter, for example:
Code:
```Please enter the first coordinates in the form x y: 1.2 3.4
Please enter the second coordinates in the form x y: 5.6 7.8
(1.2, 3.4)
(5.6, 7.8)

Process returned 0 (0x0)   execution time : 22.090 s
Press any key to continue.```
So it works for me. If you have any information besides "it doesn't ask the user" it would help. Popular pages Recent additions assignment function, cartesian class, extraction operator, operator overloading 