Last active
October 22, 2015 07:50
-
-
Save zet4/e9d5c1777ca3c65ca0d5 to your computer and use it in GitHub Desktop.
Sanitize with Templates and Callbacks.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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