使用isalpha进行循环验证

时间:2016-07-08 11:25:38

标签: c while-loop alphabetical

我在C中制作一个涉及4个选项的程序。 但是,我无法验证以检查变量Option的值是否是1,2,3或4以外的字母或数字。当我输入一个字母时,它继续循环print语句而不是输入函数而我我无法继续我的计划。 有人可以告诉我我的代码有什么问题吗?

int Option;

while( (Option>4) || ( isalpha(Option) ) )


{

printf("Please select a valid option from the 4 options listed above! \n");

scanf(" %d",&Option);

}

1 个答案:

答案 0 :(得分:3)

description for the function isalpha()表示

  

在" C" locale,isalpha仅对isupper或islower为true的字符返回true。

这意味着

isalpha('4') // false
isalpha(4) // false in ASCII-based computers
           // the ASCII table assigns 4 to a control character
isalpha('A') // true
isalpha(65) // true in ASCII-based computers
            // the ASCII table assigns 65 to the symbol 'A'