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

These won't fix your issue, but they will clean up your code a bit.

In main.cpp:
-Change lines 8-10 to:

float mypointx, mypointy, Answer;

-After line 22 add:

system("pause");

In XYPoint.h:
-Change lines 13-17 to:

float mypointx, mypointy, pointpx, pointpy, mydistance;

In XYPoint.cpp:
-Remove lines 6-10 (Variables already zero. For one run, you won't need to set them. Keep though if looping program).

-------------------------------

Not sure why you are getting the answer in scientific notation.
When you are testing your program, make the (actual) distance between points "0", as to say that both sets of coordinates are the same. If it does not turn out zero, something is wrong.

When that is working, you will need to test the logic of your program with a 3-4-5 triangle. From your fixed point (eg [0, 0]), you will go out 3 on the x, and up 4 on the y, giving you [3, 4]. The hypotenuse (the distance) of which will be "5".
http://www.tpub.com/math1/20f.htm

Hope this helps :) (If so, then please upvote!)

SgtMe,

First off thank you very much for your input. I tried the idea of making the actual distance between the two points 0, and still I was getting an answer in scientific notation. I feel as though my problem line is in the .cpp file, specifically,

mydistance=sqrt( pow (mypointx-pointpx,2)  + pow (mypointy-pointpy,2) );

I tried changing pointpx to temp.pointpx, and p.pointpx - all seem valid, and don't return any errors, but still the answer comes out in scientific notation. I am truly at a loss as to where the mistake is coming from. Something tells me it is a minor glitch, but for the past few hours, combing through the code, I just don't know what else to try.

If we break down the logic in that line, it is correct.

sqrt(
pow(mypointx-pointpx, 2)
+
pow(mypoint-pointpy, 2)
)
;

Have a look on google and see if you can find anything on converting scientific to decimal.

Thanks again for your reply -

I think it is a syntax problem, and not an issue of converting from Sci notation to decimal. The reason being, regardless of what I set the fixed point to, the answer always comes out to the exact same scientifically notated number. I don't know where it's coming from though.

Thanks again!

Have you tried enclosing the whole thing (after the equals) in "float(...);"? Sometimes that works...

Just tried that to no success, thanks for the suggestion though

Woohooo I've figured it out!

Turns out my problem was initializing. Apparently, when you get large obscure scientific notation, it probably means you haven't initialized correctly. All I did was add the following lines to main

First.pointpx=-23.5;
      First.pointpy=37.9;

..and the program worked. Thanks for sticking with me SgtMe!

Thats ok :)
Glad you got it sorted.

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.