How about this?
Code:
int IntegerInputHandler( int lengths[], int index ) {
int choice = 0;
while ( !( std::cin >> choice ) || choice > lengths[index] ) {
std::cin.clear();
std::cin.ignore( 256, '\n' );
std::cerr << "Bad Input, Retype> ";
}
return choice;
}
int main() {
int lengths[] = {4,100000};
int choice = IntegerInputHandler( lengths, 1);
std::cout << choice << '\n';
}
I think it's better to pass the length instead of an array and an index. That way you can use the function even if you don't have an array, and the fewer parameters you have, the fewer parameters you have to remember.
Code:
int GetInteger( int max ) {
int rv = 0;
while ( !( std::cin >> rv ) || rv > max ) {
std::cin.clear();
std::cin.ignore( 256, '\n' );
std::cerr << "Bad Input, Retype> ";
}
return rv;
}
int main() {
int lengths[] = {4,100000};
int choice = GetInteger( lengths[1] );
std::cout << choice << '\n';
}