#include <iostream>
#include <limits>
int main()
{
int input;
char c; //used for checking left over characters in buffer
std::cout << "Please enter an integer: ";
while( !(std::cin >> input) || //check if there are bad flags set
( std::cin.get( c ) && c != '\n' ) ) //check if anything left in buffer
{
std::cin.clear(); //clear error flags
std::cin.ignore( std::numeric_limits<std::streamsize>::max() , '\n' ); //ignore anything left in buffer
std::cerr << "Invalid input. << std::endl << Please try another integer: ";
}
std::cout << "You entered: " << input << std::endl;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8bGltaXRzPgoKaW50IG1haW4oKQp7CglpbnQgaW5wdXQ7CgljaGFyIGM7IC8vdXNlZCBmb3IgY2hlY2tpbmcgbGVmdCBvdmVyIGNoYXJhY3RlcnMgaW4gYnVmZmVyCglzdGQ6OmNvdXQgPDwgIlBsZWFzZSBlbnRlciBhbiBpbnRlZ2VyOiAiOwoJd2hpbGUoICEoc3RkOjpjaW4gPj4gaW5wdXQpIHx8IC8vY2hlY2sgaWYgdGhlcmUgYXJlIGJhZCBmbGFncyBzZXQKCSggc3RkOjpjaW4uZ2V0KCBjICkgJiYgYyAhPSAnXG4nICkgKSAvL2NoZWNrIGlmIGFueXRoaW5nIGxlZnQgaW4gYnVmZmVyCgl7CiAgICAJc3RkOjpjaW4uY2xlYXIoKTsgLy9jbGVhciBlcnJvciBmbGFncyAKICAgIAlzdGQ6OmNpbi5pZ25vcmUoIHN0ZDo6bnVtZXJpY19saW1pdHM8c3RkOjpzdHJlYW1zaXplPjo6bWF4KCkgLCAnXG4nICk7IC8vaWdub3JlIGFueXRoaW5nIGxlZnQgaW4gYnVmZmVyCiAgICAJc3RkOjpjZXJyIDw8ICJJbnZhbGlkIGlucHV0LiA8PCBzdGQ6OmVuZGwgPDwgUGxlYXNlIHRyeSBhbm90aGVyIGludGVnZXI6ICI7Cgl9CglzdGQ6OmNvdXQgPDwgIllvdSBlbnRlcmVkOiAiIDw8IGlucHV0IDw8IHN0ZDo6ZW5kbDsKCXJldHVybiAwOwp9