Alright, I've got this program that is supposed to ask the user for input of two ordered pairs, then use a class and a + operator to produce the midpoint. Unfortunately, whenever it is run, it gets the ordered pairs and then invariably says the midpoint is
(4370432,4370432). Here is the code:
Code:
//Overloading Operators
//Midpoint
#include <iostream>
using namespace std;
class CMidpoint {
public:
int x , y;
CMidpoint() {};
CMidpoint( int , int );
CMidpoint operator + ( CMidpoint );
};
CMidpoint::CMidpoint( int a , int b )
{
x = a;
y = b;
}
CMidpoint CMidpoint::operator + ( CMidpoint param )
{
CMidpoint temp;
temp.x = x + param.x;
temp.y = y + param.y;
return (temp);
}
int main()
{
CMidpoint c;
int i , j , k , l;
cout << "Please enter the x and y value (separated by a space) of an ordered pair: " << endl;
cin >> i >> j;
cin.ignore();
CMidpoint a ( i , j );
cout << "Please enter the x and y value (separated by a space) of another ordered pair: " << endl;
cin >> k >> l;
CMidpoint b ( k , l );
cout << "Your ordered pairs:" << endl;
cout << "\t(" << i << "," << j << ")" << endl;
cout << "\t(" << k << "," << l << ")" << endl;
cout << "(Press any key to confirm...)" << endl;
cin.get();
cin.ignore();
a + b = c;
cout << "The midpoint of the points is ";
cout << "(" << c.x << "," << c.y << ")." << endl;
cin.get();
return 0;
}
How can I get it to display the actual midpoint, rather than some random number?