Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- template<typename T>
- void checkInput(T& x) {
- while(cin.fail()) {
- std::cin.clear(); //Clear all the flags
- std::cin.ignore(std::numeric_limits<std::streamsize>::max(), 'n'); // Throw away the rest of the line
- std::cout<<"Wrong entry type, enter the correct type input as suggested : " << std::endl;
- std::cin >> x;
- }
- }
- int main() {
- char ch;
- std::cout<<"Input a single Character, followed by : ";
- std::cin >> ch; checkInput(ch);
- std::cout<<std::endl<<"User input character is t" << ch;
- }
- Input a single Character, followed by : abc
- User input character is a
- Input a single Character, followed by : 123
- User input character is 1
Add Comment
Please, Sign In to add comment