Will someone tell me why this will only return the value 0? Also, how are you supposed to work with situations where floats or doubles are negative? I tried making these doubles signed but the compiler gave me an error saying that cannot be done. that makes little sense.
btw this is for an actual homework assignment that I'm doing, it seems like it should be working. If you don't know already pow() simply calculates param1 to the param2 power.
Code:
//LESSON10, PROJECT 10-4,
//PAGE 202
//CHARLES THIBAULT
//THIS PROGRAM SIMPLY RETURNS THE LENGTH OF A LINE
#include <iostream>
#include <math.h>
#include <conio.h>
using namespace std;
typedef struct {
double X;
double Y;
}point_t;
typedef struct {
double X;
double Y;
double Z;
}point_t3d;
double GetLength(point_t&, point_t&); //PASSES IN COORDS BY REFERENCE
double GetLength3d(point_t3d&, point_t3d&);
int main(void) {
point_t p1;
point_t p2;
point_t3d p13d;
point_t3d p23d;
double length2d;
double length3d;
cout << "Enter the coords for your first point " << endl;
cin >> p1.X >> p1.Y;
cout << "Enter the coords for your second point " << endl;
cin >> p2.X >> p2.Y;
length2d = GetLength(p1, p1);
cout << "The length of the 2d line is " << length2d;
getch();
system("cls");
cout << "Enter coords for the first 3d line " << endl;
cin >> p13d.X >> p13d.Y >> p13d.Z;
cout << "Enter the coords for the second 3d line " << endl;
cin >> p23d.X >> p13d.Y >> p13d.Z;
length3d = GetLength3d(p13d, p23d);
cout << "The length of the 3d line is " << length3d << endl;
return 0;
}
double GetLength(point_t & p1, point_t & p2) {
double length = sqrt( (pow((p2.X - p1.X), 2)) +
(pow((p2.Y - p1.Y), 2)) );
return length;
}
double GetLength3d(point_t3d &p1, point_t3d & p2) {
double length = sqrt( (pow((p2.X - p1.X), 2)) +
(pow((p2.Y - p1.Y), 2)) +
(pow((p2.Z - p1.Z), 2)) );
return length;
}
I didn't comment it because it is fairly small thx
EDIT:
I rewrote it slightly differently using floats and it seems to be working, but i would still like to know why my previous version did not work. I think it might ahve something to do with the pow() function, i left that out in my newer version.