无法执行scanf?????/

编译打包和其他
回复
hanis_ghost
帖子: 41
注册时间: 2015-06-19 21:16

无法执行scanf?????/

#1

帖子 hanis_ghost » 2016-07-01 17:21

我用c写了一个简单的计算器,第28行scanf不能正常执行,以下是代码,请问怎么回事?谢谢
(用goto的目的是希望计算完之后询问用户是否进行第二次运算,还是退出)

#include<stdio.h>
#include<ctype.h>
int main(void)
{
char linn;
head1:
{char symbol;
float num1,num2,result,yushu;
printf("please input a 算式\n");
scanf("%f %c %f",&num1,&symbol,&num2);
if(symbol=='+'||symbol=='-'||symbol=='*'||symbol=='/')
switch (symbol)
{
case '+':
result=num1+num2;break;
case '-':
result=num1-num2;break;
case '*':
result=num1*num2;break;
case '/':
{result=num1/num2;break;}
}
else
printf("you have input a wrong symbol\n");
printf("%f\n",result);}
printf("if you want to calculate again please input y or Y\n"
"if you do not want,please input n or N\n");
scanf("%c",&linn); //就是这个scanf无发执行!!!!!
if(tolower(linn)=='y')
goto head1;
else if(tolower(linn)=='n')
printf("goodbye......\n");
}
rosynirvana
帖子: 893
注册时间: 2011-02-14 17:46

Re: 无法执行scanf?????/

#2

帖子 rosynirvana » 2016-07-01 18:10

没仔细看,但十有八九是读了缓冲区残留的那个回车
头像
astolia
论坛版主
帖子: 6420
注册时间: 2008-09-18 13:11

Re: 无法执行scanf?????/

#3

帖子 astolia » 2016-07-01 20:27

如二楼所说,linn中读到的是上一个scanf时所输入的'\n'。在这之前用getchar()把那个换行符从缓冲区里弄掉就是了
回复