永发信息网

C语言求助,为什么输入某个值后以下程序就会陷入无限循环??

答案:6  悬赏:80  手机版
解决时间 2021-03-02 01:54
编译没有问题,正常运行也没有问题,但是“Enter first number: “打印出来后,如果接着输入一个非数字,程序并没有按我设定的 出现提示后允许重新输入,而是无限打印后面printf()里的提示语句,为什么??

#include
char get_first(void);
int main(void)
{
char ch,n;
float num1,num2;
double value;
printf("Enter the operation of your choice:\n");
printf("a. add s. subtract\n");
printf("m. multiply d.divide\n");
printf("q. quit\n");

while((ch=get_first())!='q')
{
if(ch!='a'&&ch!='s'&&ch!='m'&&ch!='d')
{
printf("Please enter a,s,m,d or q:\n");
continue;
}
printf("Enter first number: ");
while(scanf("%f",&num1)!=1)
{
printf("one is not an number.\n");
printf("Please enter a number ,such as 2.5,-1.78E8 or 3: ");
continue;
}
printf("Enter the second number: ");
while(scanf("%f",&num2)!=1)
{
printf("one is not an number.\n");
printf("Please enter a number,such as 2.5,-1.78E8 or 3: ");
continue;
}
switch(ch)
{
case 'a' : n='+';
value=num1+num2;
break;
case 's' : n='-';
value=num1-num2;
break;
case 'm' : n='*';
value=num1*num2;
break;
case 'd' : n='/';
value=num1/num2;
break;

}

printf("%.1f%c%.1f=%.2f\n\n",num1,n,num2,value);

printf("Enter the operation of your choice:\n");
printf("a. add s. subtract\n");
printf("m. multiply d.divide\n");
printf("q. quit\n");

fflush(stdin);

}
printf("Bye.\n");
return 0;
}

char get_first(void)
{
char ch;
ch=getchar();
while(getchar()!='\n')
continue;
return ch;
}

实在不明白,求高手指点!
printf("Enter first number: ");
while(scanf("%f",&num1)!=1)
{
printf("one is not an number.\n");
printf("Please enter a number ,such as 2.5,-1.78E8 or 3: ");
continue;
}
这一段应该没问题啊,为什么会这样???
最佳答案
我百科了一个scanf发现我错了。。他返回的值是他读入流的个数。 被你扫盲了。然后我调试了一下,没有发现问题,不知道你碰到的是什么问题。
全部回答
你那个printf是在循环里面了,循环多少次就打印多少次的printf
意思不对呀! 输入一组整数,怎能以@结尾呢?@ 哪里是整数嘛? 你的意思是不是:要求某一次输入的整数数值等于@的ascii码才结束呀? 整数只能是一个一个的输入啦! #define n 5 void main() { int i,a[n]; printf("\n请输入%d个数:",n); for(i=0;i
  • 3楼网友:过活
  • 2021-03-02 01:29
我想知道你这个scanf("%f",&num1)!=1的意思是?
  • 4楼网友:持酒劝斜阳
  • 2021-03-02 00:41
while(scanf("%f",&num1)!=1) 应该是这句判断错误,(scanf("%f",&num1)!=1) 输入一个非数字好像是一个乱码 ,试了下,就是这句会导致无限循环,具体原因不知道。
  • 5楼网友:持酒劝斜阳
  • 2021-03-02 00:12
#include char get_first(void); int main(void) { char ch,n; float num1,num2; double value; printf("Enter the operation of your choice:\n"); printf("a. add s. subtract\n"); printf("m. multiply d.divide\n"); printf("q. quit\n"); while((ch=get_first())!='q') { if(ch!='a'&&ch!='s'&&ch!='m'&&ch!='d') { printf("Please enter a,s,m,d or q:\n"); continue; } printf("Enter first number: "); if(scanf("%f",&num1)!=1)//***************while改成 if 就对了********************** { printf("one is not an number.\n"); printf("Please enter a number ,such as 2.5,-1.78E8 or 3: "); continue; } printf("Enter the second number: "); if(scanf("%f",&num2)!=1) //***************while改成 if 就对了********************** { printf("one is not an number.\n"); printf("Please enter a number,such as 2.5,-1.78E8 or 3: "); continue; } switch(ch) { case 'a' : n='+'; value=num1+num2; break; case 's' : n='-'; value=num1-num2; break; case 'm' : n='*'; value=num1*num2; break; case 'd' : n='/'; value=num1/num2; break; } printf("%.1f%c%.1f=%.2f\n\n",num1,n,num2,value); printf("Enter the operation of your choice:\n"); printf("a. add s. subtract\n"); printf("m. multiply d.divide\n"); printf("q. quit\n"); fflush(stdin); } printf("Bye.\n"); return 0; } char get_first(void) { char ch; ch=getchar(); while(getchar()!='\n') continue; return ch; } 我在上面标注了 把2处的while改成 if 就对了 我运行过了 改过之后 可以正常使用了
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯