我用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");
}
无法执行scanf?????/
-
- 帖子: 41
- 注册时间: 2015-06-19 21:16
-
- 帖子: 893
- 注册时间: 2011-02-14 17:46
Re: 无法执行scanf?????/
没仔细看,但十有八九是读了缓冲区残留的那个回车
- astolia
- 论坛版主
- 帖子: 6444
- 注册时间: 2008-09-18 13:11
Re: 无法执行scanf?????/
如二楼所说,linn中读到的是上一个scanf时所输入的'\n'。在这之前用getchar()把那个换行符从缓冲区里弄掉就是了