User input validation that might be unbreakable for C++ I am new to programming and have been having a lot of trouble with user input validation. Everything I found online I could break and I finally put this function together that appears to be unbreakable. It is for int and double specifically.

I am curious about how it might be critiqued by experienced programmers. How did I do? What can be improved? What are some issues I might run into? One issue I ran into was when I #include&lt;Windows.h&gt;, max() becomes an issue in this code, so I had to use #define NOMINMAX. I wonder if there is a way around that as well. I am using this code with Visual C++ in Visual Studio.

#include&lt;iostream&gt;
#include&lt;string&gt;    
#include&lt;limits&gt;

using namespace std;

template&lt;class T&gt;
void input(T&amp; input, int low, int high, string message)
{
input = low - 1;
while (input &lt; low || input &gt; high)
{
    cout &lt;&lt; message;
    while ((cin &gt;&gt; input).fail() || cin.peek() != '\n')
    {
        cin.clear();
        cin.ignore(numeric_limits&lt;streamsize&gt;::max(), '\n');
        cout &lt;&lt; message;
    }
    cin.clear();
    cin.ignore(numeric_limits&lt;streamsize&gt;::max(), '\n');
}
}
The #define NOMINMAX issue is easy to fix. To do so we need to remove(or move internally to our input function) the using namespace std; call as this pollutes our global namespace with max/min calls. See this for why it is bad practice to include the namespace.
Seeing as we only want to output our message string and do not care about copying, it is best to pass this to our function as const&amp; std::string
I would change the function signature to make it easier to use in client code. Instead of passing in a temp var for input, just let the function return type T.
The type of low and high should be of the same type as T, not always int. For types such as double and float the comparisons may not always return expected results and may also need to be compared with some small epsilon value.
Finally we do not wish to modify low or high and should also pass them by const.


The final function might then look something like

#include&lt;iostream&gt;
#include&lt;string&gt;    
#include&lt;limits&gt;

template&lt;class T&gt;
T ValidateInput(const T low, const T high, const std::string&amp; message)
{
    using namespace std;

    T input = low - 1;

    while (input &lt; low || input &gt; high)
    {
        cout &lt;&lt; message;
        while ((cin &gt;&gt; input).fail() || cin.peek() != '\n')
        {
            cin.clear();
            cin.ignore(numeric_limits&lt;streamsize&gt;::max(), '\n');
            cout &lt;&lt; message;
        }
        cin.clear();
        cin.ignore(numeric_limits&lt;streamsize&gt;::max(), '\n');
    }

    return input;
}