/*************************************************************************/
/** Q99403 */
/** The distance between points on the coordinate plane. */
/*************************************************************************/
#include <iostream>
#include <cmath>
using namespace std;
double inputDouble(string); // Input from the console of type double. Check for simple input errors.
int main()
{
/* The coordinates of the point. Structure.*/
struct Point
{
double x;
double y;
};
Point p1, p2; // Declare two points.
cout << "Please enter the coordinates of the first point." << endl;
/* Enter the coordinates of the points. */
p1.x = inputDouble("x1 = ");
p1.y = inputDouble("y1 = ");
p2.x = inputDouble("x2 = ");
p2.y = inputDouble("y2 = ");
/* We use the formula to calculate the distance between points. Output the result. */
cout << endl<< "For points (" << (double)p1.x << ", " << p1.y << ") and (" << p2.x << ", " << p2.y;
cout << "), pointsDistance is " << sqrt((pow((p2.x-p1.x),2))+(pow((p2.y-p1.y),2))) << "." << endl;
return 0;
}
/** Function for entering a value of type "double" with validation.*/
/** The string in the argument is needed to prompt for data entry. */
double inputDouble(string s)
{
double d;
do{
cout << s;
cin >> d;
if(!cin) //If incorrect characters are entered, then clear the input buffer and ask to enter again.
{
cout << "The input is not correct. Try again.\n";
cin.clear();
while (cin.get() != '\n');
}
else break;
}while(true);
return d;
}
Comments
Leave a comment