The
#define NOMINMAX
issue is easy to fix. To do so we need to remove(or move internally to our input function) theusing namespace std;
call as this pollutes our global namespace with max/min calls. See this 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 asconst& 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 alwaysint
. For types such asdouble
andfloat
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<iostream>
#include<string>
#include<limits>
template<class T>
T ValidateInput(const T low, const T high, const std::string& message)
{
using namespace std;
T input = low - 1;
while (input < low || input > high)
{
cout << message;
while ((cin >> input).fail() || cin.peek() != '\n')
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << message;
}
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
return input;
}
The
#define NOMINMAX
issue is easy to fix. To do so we need to remove(or move internally to our input function) theusing 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 asconst& 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 alwaysint
. For types such asdouble
andfloat
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<iostream>
#include<string>
#include<limits>
template<class T>
T ValidateInput(const T low, const T high, const std::string& message)
{
using namespace std;
T input = low - 1;
while (input < low || input > high)
{
cout << message;
while ((cin >> input).fail() || cin.peek() != '\n')
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << message;
}
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
return input;
}
The
#define NOMINMAX
issue is easy to fix. To do so we need to remove(or move internally to our input function) theusing 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 asconst& 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 alwaysint
. For types such asdouble
andfloat
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<iostream>
#include<string>
#include<limits>
template<class T>
T ValidateInput(const T low, const T high, const std::string& message)
{
using namespace std;
T input = low - 1;
while (input < low || input > high)
{
cout << message;
while ((cin >> input).fail() || cin.peek() != '\n')
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << message;
}
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
return input;
}
1.) 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.
2.) 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& std::string
3.) 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.
4.) 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.
5.) Finally we do not wish to modify low or high and should also pass them by const
.
The
#define NOMINMAX
issue is easy to fix. To do so we need to remove(or move internally to our input function) theusing 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 asconst& 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 alwaysint
. For types such asdouble
andfloat
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
.
1.) 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.
2.) 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& std::string
3.) 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.
4.) 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.
5.) Finally we do not wish to modify low or high and should also pass them by const
.
The
#define NOMINMAX
issue is easy to fix. To do so we need to remove(or move internally to our input function) theusing 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 asconst& 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 alwaysint
. For types such asdouble
andfloat
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
.
1.) 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.
2.) 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& std::string
3.) 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.
4.) 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.
45.) 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<iostream>
#include<string>
#include<limits>
template<class T>
voidT ValidateInput(T& input, const T low, const T high, const std::string& message)
{
using namespace std;
T input = low - 1;
while (input < low || input > high)
{
cout << message;
while ((cin >> input).fail() || cin.peek() != '\n')
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << message;
}
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
return input;
}
1.) 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.
2.) 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& std::string
3.) 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.
4.) 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<iostream>
#include<string>
#include<limits>
template<class T>
void ValidateInput(T& input, const T low, const T high, const std::string& message)
{
using namespace std;
input = low - 1;
while (input < low || input > high)
{
cout << message;
while ((cin >> input).fail() || cin.peek() != '\n')
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << message;
}
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
}
1.) 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.
2.) 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& std::string
3.) 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.
4.) 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.
5.) 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<iostream>
#include<string>
#include<limits>
template<class T>
T ValidateInput(const T low, const T high, const std::string& message)
{
using namespace std;
T input = low - 1;
while (input < low || input > high)
{
cout << message;
while ((cin >> input).fail() || cin.peek() != '\n')
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << message;
}
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
return input;
}