Hi, everyone. I'm having trouble trying to build a function which validates user input as follow... I think I know where the problem is, but I have no idea how to fix it.
So in the main function I declare a string, an int and a boolean condition, with which I call the getInt function, which would validate the input. Inside the getInt function I enter a string and I check that.
Then I assign the contents of the string to num, by converting it with stoi. I do that in order for the condition to work, because otherwise "num" wouldn't have a value. And after that it's straightforward.
Everything works great but I feel like the condition doesn't do its job, because whatever I input I always get the prompt printed out or if I change the condition, every single number goes through and that still is not what I want.
#include <iostream>
template< typename CONDITION > int get_int( std::string prompt, CONDITION cond )
{
std::cout << prompt << ": " ;
int v ;
if( std::cin >> v && cond(v) ) return v ; // ok, return value entered by the user
std::cin.clear() ; // clear possible error state
std::cin.ignore( 1000, '\n' ) ; // throw away characters in the input buffer
std::cerr << "invalid input. try again\n" ; // inform the user of the error
return get_int( prompt, cond ) ; // and try again
}
int main()
{
int v = get_int( "enter an integer between 1 and 10", // prompt
[]( int v ) { return v > 0 && v < 11 ; } ) ; // condition
std::cout << "you entered: " << v << '\n' ;
}
Thanks for answering, your code works, but the problem is that when you enter something like "2asfasf" it will take "2" and still act as if the input is correct and the program will terminate with no error.
#include <iostream>
#include <string>
#include <algorithm>
template< typename CONDITION > int get_int( std::string prompt, CONDITION cond )
{
std::cout << prompt << ": " ;
std::string line ;
std::getline( std::cin, line ) ;
try // stoi may throw on error
{
std::size_t pos ;
constint v = std::stoi( line, std::addressof(pos) ) ;
// if the entire line was consumed (unparsed part is all white space)
if( std::all_of( line.begin()+pos, line.end(), [] ( char c ) { return std::isspace(c) ; } )
&& cond(v) ) return v ; // and the condition is satisfied
}
catch( const std::exception& ) {}
std::cerr << "invalid input '" << line << "'. try again\n" ; // inform the user of the error
return get_int( prompt, cond ) ; // try again
}
int main()
{
int v = get_int( "enter an integer between 1 and 10", // prompt
[]( int v ) { return v > 0 && v < 11 ; } ) ; // condition
std::cout << "you entered: " << v << '\n' ;
}