#include <iostream>
#include <string>
#include <limits>

using namespace std;

void get_non_neg_num(double & x,
                     const string & prompt,
                     const string & gen_err, const string & num_err,
                     const string & non_neg_err,
                     const string & re_prompt);

int main(void)
{
    double x;
    get_non_neg_num(x, "Enter value for x:  ", "\n\aEntry must be ",
                    "a number", "non-negative",
                    "!\n\nPlease try again:  ");
    cout << "\n\nFound '" << x << "'...\n";
    return 0;
}

void get_non_neg_num(double & x,
                     const string & prompt,
                     const string & gen_err, const string & num_err,
                     const string & non_neg_err,
                     const string & re_prompt)
{
    cout << prompt;
    cin >> x;
    while (cin.fail() || x < 0)
    {
        cout << gen_err;
        if (cin.fail())
        {
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
            cout << num_err;
        }
        else
        {
            cout << non_neg_err;
        }
        cout << re_prompt;
        cin >> x;
    }
    return;
}

