Calculate Euclidean Distance Between Two Points

Nov 8, 2015 at 11:48pm
Can anyone tell me what is going wrong in my code?
When I run it, the console shows nothing.
All help is appreciated.

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
#include<iostream>
#include<conio.h>
#include<math.h>

using namespace std;

double distanceCalculate(double x1, double y1, double x2, double y2)
{
	double x = x1 - x2; //calculating number to square in next step
	double y = y1 - y2;
	double dist;

	dist = pow(x, 2) + pow(y, 2);       //calculating Euclidean distance
	dist = sqrt(dist);                  

	return dist;
}

int main()
{
	double x1, y1, x2, y2;
	double dist;
	cout << "Enter x1:" << endl;    //user inputs the points
	cin >> x1;
	cout << "Enter y1:" << endl;
	cin >> y1;
	cout << "Enter x2:" << endl;
	cin >> x2;
	cout << "Enter y2:" << endl;
	cin >> y2;

	dist = distanceCalculate(x1, y1, x2, y2);    //initiate equation
	cout << "Distance Between (" << x1 << " , " << y1 << ") and (" << x2 << " , " << y2 << ") = " << dist;
}
Last edited on Nov 8, 2015 at 11:48pm
Nov 9, 2015 at 12:11am
hi,

Works fine for me, your console is closing before you see anything, have a read of the console closing down topic which stickied to the beginners forum .

:+)

Edit:

Try running it with the gear icon top right of your code.
Last edited on Nov 9, 2015 at 12:19am
Nov 9, 2015 at 2:51am
closed account (48T7M4Gy)
Sounds like an old console is open. Maybe clean rebuild project.
Nov 9, 2015 at 9:24am
The console showing nothing could be as @kemort said, leftover trash from a previous project. What compiler are you running? If you went with VS2010, just restart it or copy your code into a completely new project and compile.

If the issue is with the console closing down too fast and you not seeing the results properly, add this bit of code right before the closing bracket in main: cin.ignore(999,'\n');

Also please add a return 0;

It really bothers me that there isn't one.
Nov 9, 2015 at 11:19am
Also please add a return 0;

It really bothers me that there isn't one.


Yep, old habits die hard - even though the standard says an implicit 0 will be returned, I still like to see the explicit return there anyway.

Regards
Nov 9, 2015 at 1:37pm
Maybe a final

cout << endl;

does the job?
Topic archived. No new replies allowed.