3D Distance Calculator

So I made this 3D Distance calculator where you enter in the coordinates of one point and then the coordinates of the other point and it gives you the distance between the two points. It then prompts you to enter in a 1 to restart so it runs the actual program 2 times. The math seems to be correct and the program works. The question is looking it over, what do you think of it and the way it is structured? Does it look messy? Is their a different way I could structure it to make it look more concise and to the point?

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
#include <iostream>
#include <cmath>

using namespace std;

double formula();

int main()
{
    double distance;

    distance = formula();

    cout << "3D Distance = " << distance;
    cout << "\n\n-Coded By S1L3NT\n\n";

    cout << "Enter 1 to start: ";
    int x;
    cin >> x;
    if (x == 1)
    {
        double distance;

    distance = formula();

    cout << "\n 3D Distance = " << distance;
    cout << "\n\n-Coded By S1L3NT\n\n";
    }





    return 0;
}



double formula()
{
   double x, y, z, x2, y2, z2, distance;


    cout << "---DISTANCE CALULATOR---\n\n";
    cout << "Enter first x then first y then first z:\n";
    cin >> x;
    cin >> y;
    cin >> z;
    cout << "Enter second x then second y then second z:\n";
    cin >> x2;
    cin >> y2;
    cin >> z2;

    distance = sqrt(pow((x - x2), 2) + pow((y - y2), 2) + pow((z - z2), 2));

    return(distance);

}
Last edited on
It looks fine, but you are making a second variable called distance, when you could just use the first one. Also you are calling formula twice. did you want the code to run twice? It seems to me anyway that it would make more sense like this:


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
#include <iostream>
#include <cmath>

using namespace std;

double formula();

int main()
{
    double distance;
    cout << "Enter 1 to start: ";
    int x;
    cin >> x;
    if (x == 1)
    {
    distance = formula();

    cout << "\n 3D Distance = " << distance;
    cout << "\n\n-Coded By S1L3NT\n\n";
    }
    return 0;
}



double formula()
{
   double x, y, z, x2, y2, z2, distance;

    cout << "---DISTANCE CALULATOR---\n\n";
    cout << "Enter first x then first y then first z:\n";
    cin >> x;
    cin >> y;
    cin >> z;
    cout << "Enter second x then second y then second z:\n";
    cin >> x2;
    cin >> y2;
    cin >> z2;

    distance = sqrt(pow((x - x2), 2) + pow((y - y2), 2) + pow((z - z2), 2));

    return(distance);

}
Last edited on
Ahh, gotcha...I just made it easier to repeat and got rid of the crap in the if statement and said :

1
2
3
4
if (x == 1)
{
main();
}
Last edited on
Topic archived. No new replies allowed.