### 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?

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859`` ``````#include #include 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:

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344`` ``````#include #include 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 :

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