C语言 要求输入一个是字母,然后如果输入错误提示重新输入

要求输入一个是字母,然后如果输入错误提示重新输入,如果输入 a s m d q 以外的东西,提示重新输入。

我把代码复制到这里,还请各位大神帮忙。
代码:

char get_ch(void)

{

char get_word;

while (!isalpha(get_word = getchar())) /*|| (get_word == 'a' || get_word!= 's' || get_word!='m' ||get_word!= 'd'||get_word!= 'q'))*/ 这里出现问题,我已经用注释

{

printf("Your input is incorrect, please input:\n");

printf("a. add \t\t s. substract\n");

printf("m. multiply\t d. divide\n");

printf("q. quit\n");

printf("Please enter your operation of your choice:\n");

while (getchar()!= '\n')

continue;

}

return get_word;

}

#include <stdio.h>
#include <ctype.h>

char get_ch(void)
{

    char get_word;
get_word = getchar();
//int flag = isalpha(get_word = getchar()); //判断是否为字母?
int flag = (get_word == 'a' || get_word == 's' || get_word =='m' ||get_word== 'd'||get_word == 'q');
while(!flag) {

flag = (get_word == 'a' || get_word== 's' || get_word=='m' ||get_word== 'd'||get_word== 'q');
if(flag)
return get_word;
printf("Your input is incorrect, please input:\n");

        printf("a. add \t\t s. substract\n");

        printf("m. multiply\t d. divide\n");

        printf("q. quit\n");

        printf("Please enter your operation of your choice:\n");

getchar(); //消除空格
get_word = getchar();
}
return get_word;
}

int main(int argc, char *argv[])
{
printf("Hello, world\n");

char ch = get_ch();

printf("%c\n",ch);
return 0;
}

温馨提示:内容为网友见解,仅供参考
第1个回答  推荐于2018-03-09
代码如下:
#include <stdio.h>
#include <ctype.h>

char get_ch(void)
{

char get_word;
get_word = getchar();
//int flag = isalpha(get_word = getchar()); //判断是否为字母?
int flag = (get_word == 'a' || get_word == 's' || get_word =='m' ||get_word== 'd'||get_word == 'q');
while(!flag) {

flag = (get_word == 'a' || get_word== 's' || get_word=='m' ||get_word== 'd'||get_word== 'q');
if(flag)
return get_word;
printf("Your input is incorrect, please input:\n");

printf("a. add \t\t s. substract\n");

printf("m. multiply\t d. divide\n");

printf("q. quit\n");

printf("Please enter your operation of your choice:\n");

getchar(); //消除空格
get_word = getchar();
}
return get_word;
}

int main(int argc, char *argv[])
{
printf("Hello, world\n");

char ch = get_ch();

printf("%c\n",ch);
return 0;
}本回答被网友采纳
相似回答