为什么不能得到正确的答案那
来源:百度知道 编辑:UC知道 时间:2024/05/09 10:56:56
#include <stdio.h>
main()
{
int fir_num;
int sec_num;
int reslut;
char oper;
system("cls");
printf("Input the first number:");
scanf("%d", &fir_num);
printf("Input the operator:");
oper = getchar(); //输入fir_num,为什么直接跳到sec_num,难道在输入fir_num时候就算oper输入了吗?
printf("Input the second number:");
scanf("%d", &sec_num);
switch(oper)
{
case '+' : reslut = fir_num + sec_num;
break;
case '-' : reslut = fir_num - sec_num;
break;
case '*' : reslut = fir_num * sec_num;
break;
case '/' : if (sec_num == 0)
{
printf("The second number can't be ZERO!\n\n");
system("pause");
exit(0);
}
else
main()
{
int fir_num;
int sec_num;
int reslut;
char oper;
system("cls");
printf("Input the first number:");
scanf("%d", &fir_num);
printf("Input the operator:");
oper = getchar(); //输入fir_num,为什么直接跳到sec_num,难道在输入fir_num时候就算oper输入了吗?
printf("Input the second number:");
scanf("%d", &sec_num);
switch(oper)
{
case '+' : reslut = fir_num + sec_num;
break;
case '-' : reslut = fir_num - sec_num;
break;
case '*' : reslut = fir_num * sec_num;
break;
case '/' : if (sec_num == 0)
{
printf("The second number can't be ZERO!\n\n");
system("pause");
exit(0);
}
else
请求输入前,先清空输入缓冲区:
用fflush(stdin);
------------------
printf("Input the first number:");
fflush(stdin);
scanf("%d", &fir_num);
printf("Input the operator:");
fflush(stdin);
oper = getchar(); //输入fir_num,为什么直接跳到sec_num,难道在输入fir_num时候就算oper输入了吗?
printf("Input the second number:");
fflush(stdin);
scanf("%d", &sec_num);