// this is main.cpp

// stuck on getting the ouput of the distance NOT to be 0 i think its someting to do with the variables
// in the distance method in the point.cpp file

// The invokeX and invokeY are set to 0,0

    cout << "The distance from invoking point (" << invokeX << "," << invokeY << ")" << endl;
    cout << "to the coordinates you entered (" << pt1.getX() << "," << pt1.getY() << ")" << endl;
    cout << "is " << invoke.distance(aPoint) << " units" << endl;

// This is where i think i am going wrong because i keep getting 0 as an output

double Point::distance(Point aPoint)
{
    float dist_x = aPoint.getX() - 0;
    cout <<  "dist x is:" << dist_x;

    //float dist_x = x - aPoint.x;
    //float dist_y = y - aPoint.y;
    //return sqrt(pow(dist_x - 0, 2) + pow(dist_y - 0, 2));
}

Recommended Answers

All 9 Replies

Above you have aPoint being passed into the distance function and you are displaying pt1's x and y values. Do you mean to pass pt1 into the function? Based on the code that you haven shown I cannot see anything else that could be causing the problem.

This all looks OK (well not incorrect, anyway) as long as you have initialised aPoint somewhere, so I'd have to say that the problem is in the Point::getX() function. Could you post the code for the Point class?

There are some other points to mention about what you have posted though:

  • You should probably pass the Point class to functions by reference-to-const. So, something like:
    double Point::distance( const Point& destinationPoint ) const;
  • The distance() function should be a const function, as above.
  • You can basically use double as your variable type inside the function (since the return type is double anyway).
  • Variable names like aPoint are generally not a great idea, use a more descriptive name like the one I've chosen above.
  • Consider changing the name of your function from distance() to distanceTo() . A user might expect distance() to look like:
    double distance( const Point& point1, const Point& point2 );
  • Why do you have the - 0 part inside the distance function?

I had the minus 0 so i could get the distance from 0,0. i was going to do the same for y but.. its not working

class Point
{
public:
    
    Point(); 

    Point(const double x, const double y);

    Point(Point & otherPoint); 

   
    double getX();
    double getY();

    void setX(const double X);
    void setY(const double Y);

    void print();

    double distance(Point destinationPoint);
    

private:
    double x;
    double y;
};

Here is something I threw together using the code you posted. This is what I think you are trying to do.

Since the distance function is within your class I used the point within the instance with the point passed into the function to determine the distance rather than hard coding 0 (I know that you just did that for testing).

#include <iostream>
#include <cmath>
using namespace std;

double sqr(double x) //made this because I hate using the pow function for powers of 2
{
	return x*x;
}

class Point
{
	public:

	Point();
	Point(const double x, const double y);
    //Point(Point &otherPoint); //not sure why you are passing reference but left this one out

    double getX();
    double getY();

	void setX(const double X); //undefined/unused in this code
	void setY(const double Y); //undefined/unused in this code

	void print(); //undefined/unused in this code

	double distance(Point destinationPoint);

	private: //just fyi this can be up at the top since classes start off private
    double _x; //put _'s before the variable name because you
    double _y; //had the constructor with the variable names
};

Point::Point()
{
	_x = 0;
	_y = 0;
}

Point::Point(const double x, const double y)
{
	_x = x;
	_y = y;
}

double Point::getX()
{
	return _x;
}

double Point::getY()
{
	return _y;
}

double Point::distance(Point destinationPoint)
{
    return sqrt(sqr(destinationPoint.getX() - _x) + sqr(destinationPoint.getY() - _y));
}


int main()
{
	Point invoke(0,0);
	Point pt1(2,2);

	cout << "The distance from invoking point (" << invoke.getX() << "," << invoke.getY() << ")" << endl;
	cout << "to the coordinates you entered (" << pt1.getX() << "," << pt1.getY() << ")" << endl;
	cout << "is " << invoke.distance(pt1) << " units" << endl;
    return 0;
}

You are correct. that is what im trying to do for the output. when i change things around for my program i get this output.

I used 2,2 as user input


Please enter two coordinates
1st coordinate: 2
2nd coordinate: 2
The coordinates are: (2, 2)

The distance from invoking point (0,0)
to the coordinates you entered (2,2)
is 0 units

so now im thinking im not doing something correct in main.

i also posted a comment where ravenous said to use const and pass by. it still kept giving me 0.


here is what i have done so far in main.cpp


i can post what i did in the point.cpp file but i just took your advice. the point.h file matches everything else and it compiles it just still gives 0 as the units

int main()
{
    Point pt1;

    double number1,number2, invokeX, invokeY;
    Point invoke(0,0);

   
    cout << "Please enter two coordinates" << endl;
    cout << "1st coordinate: ";
    cin >> number1;

    cout << "2nd coordinate: ";
    cin >> number2;


    pt1.setX(number1);
    pt1.setY(number2);
    pt1.print();

    cout << endl;




Point destinationPoint(invokeX, invokeY);



// stuck on getting the ouput of the distance NOT to be 0 i think its someting to do with the variables
// in the distance method in the point.cpp file

cout << "The distance from invoking point (" << invokeX << "," << invokeY << ")" << endl;
cout << "to the coordinates you entered (" << pt1.getX() << "," << pt1.getY() << ")" << endl;
cout << "is " << invoke.distance(destinationPoint) << " units" << endl;


    

    return 0;
}

The problem is because you are setting your "destination point" on line 26 to invokeX/Y (0,0) and getting the distance from the invoke point (0,0). So either I would delete line 26 and replace destinationPoint on line 35 with pt1.

You sir are awesome, that fixed it. Thank you so much!!

This was my first post. it said i have no reputation points to give but if i could i would.

I know that this thread is closed, but I thought that this is worth explaining:

//Point(Point &otherPoint); //not sure why you are passing reference but left this one out

This is a copy constructor, every class has one, although the argument is usually a reference-to-const instead of a regular reference:

Point( const Point& originalPoint );

If you don't write one, the compiler will attempt to make one for you. This will basically be an element-wise copy of the member variable values. Depending on the kind of variables that your class has, this may or may not behave as you'd expect. For example, if you have member variables that are pointers to things, then the default constructor will just copy the value of the pointer, not the data that it points to. This is almost certainly not what you'd want.

If you don't plan for the copy constructor to ever be used, simply deleting it won't do, since the compiler will just make the default one. The way to make sure it's not used is to declare it as a private function, then any attempt to use it will give you a compilation error. So, for example:

class A{
};

class B{
    private:
        B(const B& ){}
};

int main()
{
    /* Make some examples of the classes */
    A a1;
    B b1;

    /* Now try and use the copy constructors */
    A a2( a1 );   // This is fine, even though one isn't declared
    B b2( b1 );   // Error! The copy constructor is a private function

    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.