I can't find a problem!! about Class

Here is my code.
I intended to compute perimeter of a triangle.
The problem is the initial value for the triangle.

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
#include <iostream>
#include <cmath>
using namespace std;

struct Point{
	double x;
	double y;
};

class Triangle
{
public:
	Point point[3];

	Triangle()
	{
		for(int i = 0; i < 3; i++){
			point[i].x = 1.0;
			point[i].y = 1.0;
		}
	}

	Triangle(Point newPoint[])
	{
		for(int i = 0; i < 3; i++)
			point[i] = newPoint[i];
	}

	double getPerimeter()
	{
		return getLength(point[1], point[2]) + getLength(point[1], point[3]) + getLength(point[3], point[2]);
	}

	double getLength(Point point1, Point point2)
	{
		return sqrt(pow((point1.x - point2.x), 2.0) + pow((point1.y - point2.y), 2.0));			
	}
};

int main()
{
	Point point[3];

	for(int i = 0; i < 3; i++)
	{
		cout << "Type x for point" << i + 1 << " : ";
		cin >> point[i].x;
		cout << "Type y for point" << i + 1 << " : ";
		cin >> point[i].y;
	}

	Triangle triangle1;
	Triangle triangle2(point);

	cout << "The perimeter of the triangle1 is : " << triangle1.getPerimeter() << endl;
	cout << "The perimeter of the triangle2 is : " << triangle2.getPerimeter() << endl;

    system("pause");
	return 0;
}


I expect to get 0 for triangle1, but I get some strange value...
can anybody tell me why this happens??

Here is the result

Type x for point1 : 1
Type y for point1 : 1
Type x for point2 : 3
Type y for point2 : 1
Type x for point3 : 1
Type y for point3 : 3
The perimeter of the triangle1 is : 2.82843
The perimeter of the triangle2 is : 6.82843
계속하려면 아무 키나 누르십시오 . . .

Last edited on
return getLength(point[1], point[2]) + getLength(point[1], point[3]) + getLength(point[3], point[2]);

Should be:

return getLength(point[0], point[1]) + getLength(point[0], point[2]) + getLength(point[2], point[1]);
Man... I was stupid ^^
Thanks for your help!!
Topic archived. No new replies allowed.