class program with friends, urgent!

I have a small debug problem, I need to create a program which takes coordinates and creates a rectangle, calculates distance and perimeter.

The problem lies in my float AB, AC, CD, BD variables, it says that each variable needs redefinition. The error lies:


1
2
3
4
5
6
7
8
float AB = Distance(A, B);
	cout << "\nAB = " << AB << endl;
	float AC = Distance(A, C);
	cout << "AC = " << AC << endl;
	float CD = Distance(C, D);
	cout << "CD = " << CD << endl;
	float BD = Distance(B, D);
	cout << "BD = " << BD << endl;


Here is the full program:

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

class POINT
{
private: int x, y;
public:
	POINT(); //set vertex to zero
	void ReadData(char vertex); //read in coordinates
	void Display(char vertex); //display coordinates for rectangle
	friend float Distance(POINT p, POINT q); //calculate distance of 2 points
	friend void DisplayPer(float AB, float AC, float CD, float BD); // calculate the perimeter 
	~POINT();
};

POINT::POINT()
{
	x = 0;
	y = 0;
}

void POINT :: ReadData(char vertex)
{
	cout << "Enter the coordinates for " << vertex << endl;
	cin >> x, y;
}

void POINT::Display(char vertex)
{
	cout << fixed << showpoint << setprecision(2);
	cout << vertex << "(" << float(x) << "," << float(y) << ")";
}

float Distance(POINT p, POINT q)
{
	return sqrt(pow(p.x - q.x, 2) + pow(p.y + q.y, 2));
}

void DisplayPer(float AB, float AC, float CD, float BD)
{
	cout << "The perimeter is = " << AB + AC + CD + BD << endl;
}

POINT :: ~POINT()
{

}

int main()
{
	POINT A, B, C, D;
	float AB, AC, CD, BD;

	cout << "For rectangle ABCD: " << endl;

	A.ReadData('A');
	B.ReadData('B');
	C.ReadData('C');
	D.ReadData('D');

	A.Display('A'); 
	for (int i = 0; i < 20; i++)
	{
		cout << char(220);
	}
	B.Display('B'); cout << endl;
	for (int i = 0; i < 3; i++)
	{
		cout << char(179) << endl;
	}
	C.Display('C');
	for (int i = 0; i < 20; i++)
	{
		cout << char(220);
	}
	D.Display('D');

	float AB = Distance(A, B);
	cout << "\nAB = " << AB << endl;
	float AC = Distance(A, C);
	cout << "AC = " << AC << endl;
	float CD = Distance(C, D);
	cout << "CD = " << CD << endl;
	float BD = Distance(B, D);
	cout << "BD = " << BD << endl;

	DisplayPer(AB, AC, CD, BD);


	system("PAUSE");
	return 0;
}
line 28: cin >> x, y; is equivalent to cin >> x;. As you do not set y, its value is undefined.

Use std::cin >> x >> y;
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
#include <iostream>
#include <cmath>
using namespace std;

class POINT
{
private:	
	double x, y; ///
	
public:

	POINT( ) {	}
	POINT( double arg1, double arg2) : x(arg1) , y(arg2) {	}
	friend double Distance (POINT p, POINT q);

};

double Distance (POINT p, POINT q)
{
	return sqrt( pow(p.x - q.x  , 2) + pow(p.y - q.y , 2) );
}

int main()
{
	double AB, AC, CD, BD; // lengths, use double
	double x, y;
	cout << "For rectangle ABCD, input coordinates\n";
	cout << "enter A coordinates (x and y with space):\n";
	cin >> x >> y;
	POINT A(x,y);	 ///

	cout << "enter B coordinates (x and y with space):\n";
	cin >> x >> y;
	POINT B(x,y);	 ///	


	AB = Distance(A,B);

	cout << "AB = " << AB << endl;	

	system("PAUSE");
	return 0;
}
The specific problem you're seeing is that you're defining AB, AC, CD, and DA at line 55, and then defining them again in the same functions at lines 81, 83, 85 and 87. By the way, DA should be BD right?
Topic archived. No new replies allowed.