I am trying to get the midpoint between two points. one of the points by default is (0,0). The other
point is entered by the user. the problem i am having is getting it to work correctly from the point class in point.cpp. it worked correctly when i put it in main.


here is a sample output

Please enter two coordinates
1st coordinate: 5
2nd coordinate: 2

The coordinates are: (5, 2)

The distance from invoking point (0,0)
to the coordinate you entered (5,2)
is: 5.38516 units

The midpoint between the invoking point (0,0)
and the coordinate you entered (5,2)
is: (0,0

if needed i can post more of what i have


this is what i have for main

#include <iostream>
#include "Point.h"
using namespace std;


int main()
{
    Point pt1;

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

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

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

    cout << endl;
    pt1.setX(number1);
    pt1.setY(number2);
    pt1.print();

    cout << endl;


// the midpoint thing below works but its not but its for testing only


    // deals with the midpoint calculations
   // midpointX = (invokeX + number1) / 2;
   // midpointY = (invokeY + number2) / 2;


  //  pt1.midpoint(midpointX);
  //  pt1.midpoint(midpointY)


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


cout << "The midpoint between the invoking point (" << invokeX << "," << invokeY << ")" << endl;
cout << "and the coordinate you entered (" << pt1.getX() << "," << pt1.getY() << ")" << endl;
cout << "is: (" << midpointX << "," << midpointY << ")" << endl;

    return 0;
}

here is the problem snippet im having trouble with

Point Point::midpoint(Point aPoint) 
{

    //midpointX = (invokeX + number1) / 2;
    //midpointY = (invokeY + number2) / 2;
    Point pt;


    // i need to do something here with pt


    return pt;
}

Line 1 - aPoint is never used.
Line 1 - Unless you intend to call a copy constructor, stick an & in there. A "const" or maybe even two might be appropriate as well. Those would be IMPROVEMENTS, but are not the actual problem.
Lines 4 and 5 - invokeX, invokeY? What are these? What they are in main is irrelevant. Get rid of them. You don't need or want them.
Lines 4 and 5 - Ditto number1 and number2? What are these? What they are in main is irrelevant. Get rid of them. You don't need or want them.

You didn't post the Point class variables. Presumably they are these?

double x; // x coordinate of the point
double y; // y coordinate of the point

Anyway, you have two Points: aPoint and "this". Calculate the x and y coordinates of the midpoints. Create a THIRD Point with these coordinates. Return it.

number1 and number2 are what i have that is passed to a distance formula that calculates distance. i put a & there.


yes the point class does have the double x and y.

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 &distancePoint);



    Point midpoint(Point &aPoint);

    

private:
    double _x;
    double _y;
};

#endif	/* POINT_H */
Point Point::midpoint(Point& aPoint) 
{

    //midpointX = (invokeX + number1) / 2;
    //midpointY = (invokeY + number2) / 2;
    Point pt;


    // i need to do something here with pt


    return pt;
}

>> number1 and number2 are what i have that is passed to a distance formula that calculates distance.

Not in here they aren't. They're declared in main, which doesn't help.

I imagine you want this...

double midpointX = (getX() + aPoint.getX()) / 2;

Do the same for y. Now create a new Point with midpointX and midpointY and return it.

There is an actual midpoint formula you can use to find the midpoint between two points.
http://www.purplemath.com/modules/midpoint.htm

So in your case, using (0,0) and (5,2)
x = (5+0)/2 and y = (2+0)/2

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.