If you want to break upon an invalid input you have to check a condition at each input. But you can use a function for each input that prints the error message:
#include <float.h>
bool getDouble(double &val, const char *msg, double min = DBL_MIN, double max = DBL_MAX)
{
bool bValid = true;
cout << endl << msg;
cin >> val;
if (cin.fail())
{
cout << endl << "Error: please enter valid components for the vectors." << endl << endl;
bValid = false;
}
else if (val < min)
{
cout << endl << "Error: Value is too small" << endl << endl;
bValid = false;
}
else if (val > max)
{
cout << endl << "Error: Value is too large" << endl << endl;
bValid = false;
}
return bValid;
}
</float.h>
Then call this function for each input:
if (!getDouble(ux, "Please enter the x-component of vector u: "))
return 1;
if (!getDouble(uy, "Please enter the y-component of vector u: "))
return 1;