多个scanf输入后面的不起作用的问题?

include <stdio.h>

int main()
{

double a,b,c,d,e;
char op;
printf("\nEnter the first complex number:");
scanf("%lf%lf",&a,&b);
printf("\nEnter the second complex number:");
scanf("%lf%lf",&c,&d);
printf("Enter the operator:");
scanf("%c",&op);
printf("%c",op);

}

这个语句未能正常执行,大家帮忙看看?

阅读 3.3k
3 个回答

你把最后 printf 的 %c 改成 %d 就看出来问题所在了

问题出在scanf("%c",&op);此行接收到的是键盘输入d值后的enter键,所以直接跳过了输入运算符operator的步骤,换句话说此程序的运算符为enter(即为'n')。

解决方法:
printf("Enter the operator:");下添加getchar();,即

printf("Enter the operator:");
getchar();
scanf("%c",&op);
printf("%c",op);

此函数的作用是“吃掉enter键”,获取键盘输入

希望对你有帮助^_^

并不是自动赋一个值,我猜你%d得到的结果是10,因为10是“换行”的ASCII。
%c是逐位读取的,不像%d %lf这样会自己去找合法的数据(这样说很不严谨但可以理解)

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进