开发者

C++, Accept lowercase and uppercase letters in a variable

开发者 https://www.devze.com 2022-12-09 20:07 出处:网络
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 metho

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;
0

精彩评论

暂无评论...
验证码 换一张
取 消