关于输入语句的问题
以下是一个程序的一部分,这个程序第一个输入是判断进入哪个函数,这里输入3,进入输入字母的函数,第二次输入五个字母,如果全为字母则结束,否则显示error并重新输入
。但为什么第一次输入完时就进行了第二次输入的判断了?,就是我输入了3然后直接显示error让重新输入,这是为什么?这个怎么处理?
main()
{
printf("1:shuzi\n2:fuhao\n3:zimu\nplease input you choice:");
scanf("%d",&cho); /*第一次输入*/
if(cho==3)
/*省略了一些内容*/
else duizhaoshuru();
}
/*省略了一些内容*/
void duizhaoshuru()
{
int n;
printf("input 5 word:");
gets(word); /*第二次输入*/
for(n=0;n<5;n++)
if(!(word[n]>='a'&&word[n]<='z'||word[n]>='A'&&word[n]<='Z'))/*判断是否全是字母*/
{
printf("Input Error! Please Input Again\n");/*如果其中有不是字母的,显示error,并重新输入*/
duizhaoshuru();
break;
}
}
[解决办法]
输入缓存没有清空,在用标准输入时,换行是会存在缓冲区的,比如
int nA = 0;
while(1)
{
printf("请输入一个数组");
scanf("%d",&nA);
printf("输入的数字是%d", nA);
}