Incorrect Math Output

I am having issues with incorrect math output for my program. I have to write a program that takes in two coordinates (for a triangle) and then prints the area and perimeter. This program also needs to include a menu. So option 1 would allow the user to enter four numbers (for the coordinates), option 2 would print the perimeter based on the coordinates entered, and option 3 would print the area.

When I enter the coordinates, the perimeter and area options give me really weird numbers in scientific notation and both the area and perimeter options return the same number. Can someone find an issue in my code that may be causing this?

Note: My program is split up into 3 files, so I apologize in advance if it's too long.

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
//Triangle.cpp

#include "Triangle.h"
#include "Main_1b.cpp"

Triangle::Triangle()
{
	double x1;
	double x2;
	double y1;
	double y2;
	double area;
	double perimeter;

void Triangle::setx1(double setx1_1)
{
	x1 = setx1_1;
}

double Triangle::getx1()
{
	return x1;
}

void Triangle::setx2(double setx2_2)
{
	x2 = setx2_2;
}

double Triangle::getx2()
{
	return x2;
}

void Triangle::sety1(double sety1_1)
{
	y1 = sety1_1;
}

double Triangle::gety1()
{
	return y1;
} 

void Triangle::sety2(double sety2_2)
{
	y2 = sety2_2;
}

double Triangle::gety2()
{
	return y2;
}

void Triangle::setPerimeter(double perimeter1)
{
	perimeter = perimeter1;
	perimeter1 = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
}

double Triangle::getPerimeter()
{
	return perimeter;
}

void Triangle::setArea(double area1)
{
	area = area1;
	area1 = ((x2 - x1) * (y2 - y1) / 2);
}

double Triangle::getArea()
{
	return area;
}

void Triangle::printPerimeter()
{
	cout << "The triangle's perimeter is: " << perimeter << endl;
}

void Triangle::printArea()
{
	cout << "The triangle's area is: " << area << endl;
}


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
//Triangle.h 

#ifndef Triangle_h
#define Triangle_h

#include <iostream>
#include <string>
#include <cmath>

using namespace std;

class Triangle
{
private:
	double x1;
	double x2;
	double y1;
	double y2;
	double area;
	double perimeter;

public:
	Triangle();
	void setx1(double x1);
	double getx1();

	void setx2(double x2);
	double getx2();

	void sety1(double y1);
	double gety1();

	void sety2(double y2);
	double gety2();

	void setPerimeter(double perimeter);
	double getPerimeter();
	void printPerimeter();

	void setArea(double area);
	double getArea();
	void printArea();


};

#endif 


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
#include "Triangle.h"
#include <string>
#include <cmath>

int Menu();

int main()
{
	
	double x1, x2, y1, y2;
	double perimeter;
	double area;
	int choice;

	choice = Menu();

	Triangle TriangleObj;

	// while ((choice >= 1) && (choice < 4)){
		
		if (choice == 1){
			cout << "Please enter the triangle's coordinates: ";
			cin >> x1 >> x2 >> y1 >> y2;
			TriangleObj.setx1(x1);
	 		TriangleObj.setx2(x2);
	 		TriangleObj.sety1(y1);
	 		TriangleObj.sety2(y2);

			if (x1 > x2){
				cout << "Invalid triangle. Please try again." << endl;
				main();
			}
			else {
				cout << "Triangle accepted." << endl;
				main();
			}
		}
		
		else if (choice == 2){
			TriangleObj.printPerimeter();
			cin.get();	

		}
		
		else if (choice == 3){
			TriangleObj.printArea();
			cin.get();
		}
		
		else if (choice >= 4){
			exit(0);
		}

}

int Menu()
{
	int choice1;

	cout << "\nPlease enter a number (1-4) to make a selection." << endl;

	cout << "\n1. Enter the triangle's information." << endl;
	cout << "2. Print the triangle's perimeter." << endl;
	cout << "3. Print the triangle's area." << endl;
	cout << "4. Exit" << endl;

	cout << "\nYour selection: ";
	cin >> choice1;
	cout << endl;
	
	return choice1;

}

Last edited on
closed account (48T7M4Gy)
A triangle has 3 points/vertices so the coordinates you require are x1,y1 x2,y2 and x3,y3.

Your perimeter calculation isn't right and I'd suggest before you work that out you write a method for working out the length of a side:

dx = xb - xa
dy = yb - ya

so Lba = sqrt( dx*dx + dy*dy)

Thanks for your reply. I know a triangle should have three sets of coordinates but my instructor only requires two for this assignment (even though that's not a triangle). But I will try to rework my math.
I know a triangle should have three sets of coordinates but my instructor only requires two for this assignment (even though that's not a triangle).


That's brilliant!

Does he/she believe the earth is flat, too?
Are you supposed to assume a third coordinate at (0,0) or what?

The other posters are correct. Two coordinates define a line. It has a length. That's all.
closed account (48T7M4Gy)
We also have an astronomy thread nearby that opens up opportunities for that too, but I digress @lastchance.

Given the OP reply and our mutual shock, I can now only imagine that the unspoken but nevertheless vital vertex is at the origin.

So @OP you will have x0 = 0.0, y0 = 0.0 This will make some of the calculations much easier for you to do but you'll need to take them carefully one at a time. The first step is to work out the length of each side.

Afetr that you'll need to check the triangle inequality to make sure you have a triangle.

If it is a triangle, the perimeter then the are follows. They don't need to be stored, getPerimeter and getArea are the way to go.

http://www.math-only-math.com/area-and-perimeter.html has a formula for the area of a triangle given the length of the 3 sides


closed account (48T7M4Gy)
This is the triangle inequality test:
http://www.mathopenref.com/triangleinequality.html

Surf around if it's not clear there are millions of versions on the net.
Ah @kemort, the area of a triangle on the surface of a sphere is much more complicated (and the angles don't add up to 180 degrees either.) But yes, I had noticed the astronomy thread!

For the area of a triangle (on a flat piece of paper) you can also use (without any square roots to find the side lengths):
A = 0.5 * abs ( (x1-x0)(y2-y0) - (x2-x0)(y1-y0) )

If (x0,y0) happens to equal (0,0) then this simplifies to
A = 0.5 * abs( x1.y2 - x2.y1 )

Quickest way to prove it is the vector area of a triangle (1/2) a x b where a and b are the side vectors.
Last edited on
Topic archived. No new replies allowed.