Help with algorithm for extraction operator

Apr 6, 2014 at 6:59am
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?

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
  #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&);

};

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;
}
Apr 6, 2014 at 7:09am
closed account (2b5z8vqX)
 
Cartesian coord1, coord2;

You are attempting the construction of two objects of type Cartesian with an undefined constructor.
Apr 6, 2014 at 7:40am
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?
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#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;
	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;
}
Apr 6, 2014 at 11:34am
closed account (2b5z8vqX)

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.

1
2
in >> num.x;
in >> num.y;

You've seemingly confused non-reference variables with reference variables. Know that they do differ.
Last edited on Apr 6, 2014 at 11:34am
Apr 6, 2014 at 5:00pm
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?
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#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;
}
Last edited on Apr 6, 2014 at 7:01pm
Apr 6, 2014 at 5:06pm
You don't need the
1
2
3
4
double c;
double d;
x=c;
y=d;
part.

Also, main should probably look something more like
1
2
3
4
5
6
7
8
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;
}
Apr 6, 2014 at 7:08pm
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?
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#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()
{
	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;
}
Apr 6, 2014 at 7:13pm
Whoops, I didn't notice at first that you had operator>> defined.
In that case, you should just be able to do
1
2
3
4
5
6
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))
    // ... 
Apr 6, 2014 at 7:33pm
Okay so I made those changes, but it still doesn't ask for user input. What am I doing wrong?
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#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;
}
Apr 6, 2014 at 9:44pm
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.