User Input Validation Help?

Write your question here.
Hi ! I am working on a code that will find the distance from a point on the Cartesian plane to the origin (0,0). As you can see this code does that. But I was wondering what I would have to do to alter the code so that the program only accepts positive numbers as x and y. I'm not great with user input validation and I was hoping someone might be able to help me out. Would it be best to use a while loop in the main portion of the code after the user inputs, x and y, or is it possible to input a while loop in the void setPoint portion of the code or in the double getPoint portions?
Thanks in advance for you help, I'm pretty new to this.



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

//Point Class Declaration:

class Point
{
private:
double x1_val;
double y1_val;
public:
void setPoint1(double);
void setPoint2(double);
double getPoint1() const;
double getPoint2() const;
double getDistance() const;
};
void Point::setPoint1(double x)
{
x1_val = x;
}

void Point::setPoint2(double y)
{
y1_val = y;
}

double Point::getPoint1() const
{
return x1_val;
}
double Point::getPoint2() const
{
return y1_val;
}

double Point::getDistance() const
{
double Distance;
Distance = sqrt(x1_val*x1_val + y1_val*y1_val);
return Distance;
}


int _tmain(int argc, _TCHAR* argv[])
{
Point coordinates;
double coordx, coordy;
double distance;
cout << "This Program will find the distance of a positive (first quadrant) point from the origin on the cartesian plane" << endl;
cout << "Please enter a positive value for 'x'" << endl;
cin >> coordx;
cout << "Please enter a positive value for 'y'" << endl;
cin >> coordy;
coordinates.setPoint1(coordx);
coordinates.setPoint2(coordy);

cout << " the distance between" << " (" << coordinates.getPoint1() << "," << coordinates.getPoint2() << ") " << "and the origin (0,0) is" << coordinates.getDistance() << endl;
return 0;
}
Topic archived. No new replies allowed.