Need help with Shortest Distance calculator Problem (3 points)

I was having a problem with the following question:

Write a C++ program that reads in three points from the keyboard (x1,y1), (x2,y2), and (x3,y3). The program then displays the shortest distance from any combination of these three points. The output of the program should be formatted to 3 decimal digits. If the shortest distance between two points is zero, the program should
instead display a message saying that at least two points are the same, and display the second shortest distance between the three points.Write your question here.

The problem i was having was that everytime i entered three points which had the same length, the program would ignore the first statement. I have bolded the else if line Im talking about. For ex, if i enter the points (1,2), (1,3), and (1,4), the program ignores the fact that the distances between these points are the same. Thanks.

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

int main()
{
	cout.setf(ios::fixed);
	cout.setf(ios::showpoint);
	cout.precision(3);

	double x1, y1, x2, y2, x3, y3, area, distance1, distance2, distance3, s;
	//Precondition: declares all variables used in the entire program
	//receives values for each point through the user and uses these values to calculate the shortest length from the triangle
	//Postcondition: the shortest length of the triangle is shown on the screen but if the shortest distance is zero
	//then the length of the second shortest side is displayed on the screen

	cout << "Enter value for x1: ";					//asks user to enter value for x1
	cin >> x1;                                  //user enters value for x1

	cout << "Enter value for y1: ";					//asks user to enter value for y1
	cin >> y1;                                  //user enters value for y1

	cout << "Enter value for x2: ";					//asks user to enter value for x2
	cin >> x2;                                  //user enters value for x2

	cout << "Enter value for y2: ";					//asks user to enter value for y2
	cin >> y2;                                  //user enters value for y2

	cout << "Enter value for x3: ";					//asks user to enter value for x3
	cin >> x3;                                  //user enters value for x3

	cout << "Enter the value for y3: ";				//asks user to enter value for y3
	cin >> y3;                                  //user enters value for y3

	distance1 = sqrt((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1));				//calculates distance from point (x1,y1) to (x2,y2)

	distance2 = sqrt((x3 - x2)*(x3 - x2) + (y3 - y2)*(y3 - y2));                //calculates distance from point (x2,y2) to (x3,y3)

	distance3 = sqrt((x3 - x1)*(x3 - x1) + (y3 - y1)*(y3 - y1));                //calculates distance from point (x1,y1) to (x3,y3)

	if (distance1 == 0 && distance2 == 0 && distance3 == 0)								//if all three lengths have a distance of zero
		cout << "All the points are the same. Please enter a new set of points.\n";		//then it outputs a message letting the user know that they are all the same and asks user to enter new points
	
	else if (distance1 == 0 || distance2 == 0 || distance3 == 0)						//if either one of the distances are the same
	{
		cout << "At least 2 of the points are the same\n";                              //then it displays a message to the user notifying that atleast two of the points are the same

		if (distance1 == 0)                                                             //if distance1 is equal to zero, then it looks at the other two
		{
			if (distance2 < distance3)												    //if distance2 is less than distance3
				cout << "The second shortest distance is " << distance2 << endl;        //then it outputs a message on the screen stating that distance2 is the second shortest
			else                                                                        //if distance3 is less than distance2
				cout << "The second shortest distance is " << distance3 << endl;        //then it outputs a message on the screen stating that distance3 is the second shortest
		}
		if (distance2 == 0)																//if distance2 is equal to zero, then it looks at the other two
		{
			if (distance1 < distance3)                                                  //if distance1 is less than distance3
				cout << "The second shortest distance is " << distance1 << endl;		//then it outputs a message on the screen stating that distance1 is the second shortest
			else                                                                        //if distance3 is less than distance1
				cout << "The second shortest distance is " << distance2 << endl;        //then it outputs a message on the screen stating that distance3 is the second shortest
		}
		if (distance3 == 0)                                                             //if distance3 is equal to zero, then it looks at the other two
		{
			if (distance1 < distance2)                                                  //if distance1 is less than distance2
				cout << "The second shortest distance is " << distance1 << endl;        //thenit outputs a message on the screen stating that distance1 is the second shortest
			else                                                                        //if distance2 is less than distance1
				cout << "The second shortest distance is " << distance2 << endl;        //then it outputs a message on the screen stating that distance2 is the second shortest
		}
	}
	else if (distance1 == distance2 == distance3)
		cout << "All of the distances are the same. " << "They all have a distance of " << distance1 << endl;
	else if (distance1 < distance3 && distance2 < distance3 && (distance1 == distance2))                                                  //if two distances are less than distance3
		cout << "Two of the distances are the same. " << "The shortest distance is " << distance1 << endl;	  //then it outputs a message on the screen stating that the two distances are the same and then displays the length of them
	else if (distance1 < distance2 && distance3 < distance2 && (distance1 == distance3))                                                 //if two distances are less than distance2
		cout << "Two of the distances are the same. " << "The shortest distance is " << distance1 << endl;    //then it outputs a message on the screen stating that these two distances are the same and then displays the length of them
	else if (distance2 < distance1 && distance3 < distance1 && (distance2 == distance3))                                                  //if two distances are less than distance1
		cout << "Two of the distances are the same. " << "The shortest distance is " << distance2 << endl;    //then it outputs a message on the screen stating that these two distances are the same and then displays the length of them

	else if (distance1 < distance2 && distance1 < distance3)                                                  //if distance1 is less than the other two
		cout << "The shortest distance from any combination of lines is: " << distance1 << endl;              //then it outputs a message on the screen stating that distance1 is the shortest
	else if (distance2 < distance1 && distance2 < distance3)												  //if distance2 is less than the other two 
		cout << "The shortest distance from any combination of lines is: " << distance2 << endl;              //then it outputs a message on the screen stating that distance2 is the shortest
	else                                                                                                     //if distance3 is less than the other two
		cout << "The shortest distance from any combination of lines is: " << distance3 << endl;             //then it outputs a message on the screen stating that distance3 is the shortest
	
	system("pause");                                                                                         //prevents the program from closing when the user enters a command

	return 0;                                                                                                //returns a value
}
  
Last edited on
For ex, if i enter the points (1,2), (1,3), and (1,4),
Distances are 1, 1 and 2 (between (1;2) and (1;4)). So it should not be caught by that if.
Yea i realized that later too. lol. but is the else if (distance1 == distance2 == distance3) legal or do i have to write it as
else if ((distance1 == distance2) && (distance1 == distance3) && (distance2==distance3))? Thanks.
is the else if (distance1 == distance2 == distance3) legal
No. You will have toc compare like distance1 == distance2 && distance2 == distance3 (third comparison is redundand as equality is transitive)

Note that you may see that even those points which should have equal distance are not caught by this, because of floating point imprecision.
Thanks for letting me know. I really appreciate it.
Topic archived. No new replies allowed.