std::getline(std::cin,input); // get a line of input
valid = true; // assume it's valid
for(auto& i : input) // check each character in the input string
if(!std::isalpha(i)) // is it an alphabetical character?
valid = false; // if not, mark it as invalid
// print an error to the user
std::cout << "Invalid input. Please input only alphabetical characters." << std::endl;
break; // break out of the for() loop, as we have already established the input is invalid
}while(!valid); // keep going until we get input that's valid
return input; // once we have valid input, return it
Brilliant! that isalpha(i) is what I needed. Thanks!
Although one more thing, suppose I want the user to input an object of type char...The thing is, I want the user to input only a single letter, in the sense if the user inputs
, an error message should be printed stating that the user has not entered a single letter.
Given that the object is not of type string but char, I don't think I could have the loop thing going on to check whether or not only one character was entered. So how do I prompt the user to input a single letter?