I/O checking. "IntegerInputHandler"
Ok, i am building an application, that will take multiple numeric input from an user, as in "choises", where we got a maximum number representing the boarder of numbers u can pick. i have made this function for it:
Code:
int IntegerInputHandler ( int lengths [], int index )
{
int maxchoise = lengths[index];
int choise = 0;
std::string Handler;
std::cout <<"InpuT:";
std::getline (std::cin,Handler);
std::stringstream (Handler) >> choise ;
while (choise > maxchoise )
{
std::cerr <<"Bad Input, Retype";
std::getline (std::cin,Handler);
std::stringstream (Handler) >> choise ;
}
return (choise);
}
Code:
int main ()
{
int lengths [] = {4,100000};
int index = 2;
IntegerInputHandler ( lengths,index );
int choise = 0;
}
it takes 2 parameters, the first one is an array with all tha maximum numbers in ,and the second parameter is the index of the array where the number is we want. but now my problem, assume that index 2 is chosen as in my example, and the user enters A, as input this will validate cause it will be converted to the ANSCII number of the letter, right? how do i prevent this, how do i prevent that annything else being input besides an int gets rejected and u will be asked to give new input....