In its current state the program validates input for data types: int and string.
//valid score range between 0 and 100
bool validScore(int value)
{
if(value < 0 || value > 100)
return false;
return true;
}
//valid name composed of letters
bool validName(string word)
{
for(int i = 0; i < word.length(); i++)
{
if(!isalpha(word[i]) )
return false;
}
return true;
}
Input is obtained through two functions for each data type: get_int() and get_word().
The validation functions are optional parameters for cases where int range is unspecified and names may contain any character.
bool get_int(const string&, int&, bool(*validScore)(int) = 0);
bool get_word(const string&, string&, bool(*validWord)(const string) = 0);
// bool get_int(const string& prompt, int&n, (*validScore)(int))
bool get_word(const string& prompt, string&word, bool(*validWord)(const string) )
{
while(1)
{
string line;
cout << prompt;
if(!getline(cin, line)
break;
istringstream iss(line);
if(iss >> word) // iss >> n
{
// validScore == 0 || validScore(n)
if(validWord == 0 || validWord(word))
return true;
}
}
return false;
}
I am wondering, if possible, how to correctly declare a function template that could streamline the process when validation is required.
template<typename T> bool get_item(const string&, T&, ???);
Would overloading the function template get_item with the different validation functions be a potential solution?
valid(const T &)
, that only has specializations for types it supports? – dwcanillas