Thread: can someone tell me why this will only return the value zero?

  1. #1
    Shadow12345
    Guest

    can someone tell me why this will only return the value zero?

    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.
    Last edited by Shadow12345; 12-04-2002 at 10:54 AM.

  2. #2
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    Does your calculations produce negative results because you cant take the sqrt of negatives?
    Mr. C: Author and Instructor

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Does your calculations produce negative results because you cant take the sqrt of negatives?
    No, if sqrt takes a negative argument the result is a domain error. I personally would be very annoyed if an invalid argument returned a valid result, wouldn't you?

    -Prelude
    My best code is written with the delete key.

  4. #4
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    GetLength(p1, p1);

    I would expect this to return 0 ;-)
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another weird error
    By rwmarsh in forum Game Programming
    Replies: 4
    Last Post: 09-24-2006, 10:00 PM
  2. Why only 32x32? (OpenGL) [Please help]
    By Queatrix in forum Game Programming
    Replies: 2
    Last Post: 01-23-2006, 02:39 PM
  3. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM
  4. opengl help
    By heat511 in forum Game Programming
    Replies: 4
    Last Post: 04-05-2004, 01:08 AM
  5. Algorithm to walk through a maze.
    By Nutshell in forum C Programming
    Replies: 30
    Last Post: 01-21-2002, 01:54 AM