Skip to content

Instantly share code, notes, and snippets.

@zet4
Last active October 22, 2015 07:50
Show Gist options
  • Save zet4/e9d5c1777ca3c65ca0d5 to your computer and use it in GitHub Desktop.
Save zet4/e9d5c1777ca3c65ca0d5 to your computer and use it in GitHub Desktop.
Sanitize with Templates and Callbacks.
#include <iostream>
#include <string>
namespace utils {
template< typename ParamType, typename Functor >
ParamType sanitize(Functor callback, std::string entryMessage) {
ParamType temp;
do {
std::cout << entryMessage;
} while(!(std::cin >> temp) || !callback(temp));
return temp;
}
}
bool checkChoice(int temp) {
return temp == 1 || temp == 2 || temp == 3;
}
int main() {
int choice = utils::sanitize<int>(checkChoice, "Enter choice: ");
std::cout << "Your choice is " << choice << '.' << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment