
C语言 要求输入一个是字母,然后如果输入错误提示重新输入
要求输入一个是字母,然后如果输入错误提示重新输入,如果输入asmdq以外的东西,提示重新输入。我把代码复制到这里,还请各位大神帮忙。代码:charget_ch(void)...
要求输入一个是字母,然后如果输入错误提示重新输入,如果输入 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;
} 展开
我把代码复制到这里,还请各位大神帮忙。
代码:
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;
} 展开
2个回答
展开全部
#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;
}
展开全部
代码如下:
#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;
}
#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;
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询