Hello there,
This is my first time posting on these forums. I was wondering if I could please get some help, there is a minor glitch in my code but I cannot figure it out. Basically, I have to create a class called XYPoint, and according to given criteria, have it calculate the distance between two points. I have a Main file, an XYPoint.cpp file, and an XYPoint.h file shown below.
Main
#include <iostream>
#include "XYPoint.h"
using namespace std;
int main()
{
XYPoint First;
float mypointx=0;
float mypointy=0;
float Answer=0;
XYPoint ();
XYPoint fixedpoint (-23.7, 37.5);
cout<<"Enter point x: "<<endl;
cin>>mypointx;
cout<<"Enter point y: "<<endl;
cin>>mypointy;
cout<<"\n";
XYPoint mypoint (mypointx, mypointy);
Answer=mypoint.distance(fixedpoint);
cout<<"\n";
cout<<"Answer is: "<<Answer<<"\n";
return 0;
} // end function main
XYPoint.h
#ifndef XYPOINT_H
#define XYPOINT_H
class XYPoint
{
public:
XYPoint();
XYPoint(float fixedpointx, float fixedpointy);
void setmypoint(float newmypointx, float newmypointy);
float distance(XYPoint p);
void displayMessage();
float mypointx;
float mypointy;
float pointpx;
float pointpy;
float mydistance;
};
#endif //
XYPoint.cpp
#include <iostream>
#include <math.h>
#include "XYPoint.h"
using namespace std;
XYPoint::XYPoint()
{
mypointx=0;
mypointy=0;
}
void XYPoint::setmypoint(float newmypointx, float newmypointy)
{
mypointx=newmypointx;
mypointy=newmypointy;
}
XYPoint::XYPoint(float fixedpointx, float fixedpointy)
{
pointpx=fixedpointx;
pointpy=fixedpointy;
}
float XYPoint::distance(XYPoint p)
{
XYPoint temp( 0, 0 );
temp.pointpx = p.pointpx;
temp.pointpy = p.pointpy;
mydistance=sqrt(pow(mypointx-pointpx,2)+pow(mypointy-pointpy,2));
return mydistance;
}
The number it prints out after compiling is something like 1.508e008, which is obviously incorrect. I cannot for the life of me figure out the source of the problem. I am using Visual C++ 2010. Your help is much appreciated