#include "stdio.h"
int main()
{
double number1 = 0.0;
double number2 = 0.0;
int operation = 0;
char type1 = 0;
restart:
printf("\nEnter the calculation :\n");
scanf("%lf %c %lf", &number1, &operation, &number2);
fflush(stdin); // 添加这个,清空输入缓存,下同
switch(operation)
{
case '+':
printf("= %lf\n",number1 + number2);
break;
case '-':
printf("= %lf\n",number1 - number2);
break;
case '*':
printf("= %lf\n",number1 * number2);
break;
case '/':
if(number2 == 0)
printf("\n\n\aDivision by zero error!\n");
else
printf("= %lf\n",number1 / number2);
break;
case '%':
if((long)number2 == 0)
printf("\n\n\aDivision by zero error!\n");
else
printf("= %ld",(long)number1 % (long)number2);
break;
default:
printf("\n\n\aIllegal operation!\n");
break;
}
printf("Enter 'Y' OR 'N' to recalculation or quit: ");
scanf("%c",&type1);
fflush(stdin); // 添加这个。
switch(type1)
{
case 'Y': case 'y':
goto restart;
break;
case 'N': case 'n':
break;
}
return 0;
}
运行结果:
你的原程序中,输入
3 + 4 (回车)
这个回车符会保存在输入缓存中,所以在你输入type1时,这个回车就默认输入了,就会直接返回了。所以要清空缓存,让你进行下一次输入。
%C 会读取后面的一个\n scanf("%c",&type1); 读两次试试
可能这个 scanf("%c",&type1); 接受的是上次输入后的回车字符