C++, Accept lowercase and uppercase letters in a variable
I want to allow the user to use lowercase or uppercase letters giving the value to the char type variable... Any help?开发者_如何学Go?
Err, do you mean something like (where getAChar()
is whatever method you're using to get the character):
int ch = getAChar();
while (!isalpha (ch))
ch = getAChar();
Alternatively, if you want to check that a user enters only alphas. You can get a string with:
cin >> myString;
Checking for alphas is as simple as:
char *cstr = myString.c_str();
for (int i = 0; i < myString.length(); i++)
if (!isalpha (*cstr++))
return false;
return true;
精彩评论