1.问题描述:张阿姨开了一个炸鸡店,想写个程序来计算客人应付的金额,偶然发现输入“ · ” 时,会停不下来。背后到底发生了什么呢?又怎样解决呢?
请你给出对应的问题分析报告,并给出你的解决方案。
鸡腿应用的源码如下:
#include <stdio.h>
int main()
{
int JITUI_num = 0;
int price_of_JITUI = 998;
double discount = 0.6;
double total = 0;
for(;;)
{
/* input */
printf("鸡腿数量:");
scanf_s("%d", &JITUI_num);
/* processing */
total = price_of_JITUI * JITUI_num * discount;
/* output */
printf("Total: %lf\n", total);
}
return 0;
}
2.原因分析:
该程序中scanf要求读取到十进制整形,而字符类型的 ` 并不匹配,因此scanf在缓冲区中读取为0,也导致 ` 在缓冲区中一直存在,因而不会发生阻塞.
解决