E
Eric Lilja
Hello, in my program I need to ask the user to input some alphabetical
letters. Case should not matter. Any input that isn't an alphabetical letter
should be rejected and the user prompted to try again.
I came up with:
std::cout << "Type a letter: ";
char c = '\0';
while(!(std::cin >> c) || !std::isalpha(c))
{
std::cerr << "That wasn't a valid alphabetical letter, please try again:
" << std::flush;
if(!std::cin)
{
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
}
It seems to work, but I was wondering if it needs more robustifying without
going to ridicolous lengths to achieve it?
/ E
letters. Case should not matter. Any input that isn't an alphabetical letter
should be rejected and the user prompted to try again.
I came up with:
std::cout << "Type a letter: ";
char c = '\0';
while(!(std::cin >> c) || !std::isalpha(c))
{
std::cerr << "That wasn't a valid alphabetical letter, please try again:
" << std::flush;
if(!std::cin)
{
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
}
It seems to work, but I was wondering if it needs more robustifying without
going to ridicolous lengths to achieve it?
/ E