【发布时间】:2012-11-12 07:14:08
【问题描述】:
#include <stdio.h>
#include <conio.h>
int main()
{
float L=0; //L is litre
float gallon;
gallon = 3.785*L;
char input;
float cost;
printf("Hello, welcome to PetrolUpHere!!\n");
printf("Would u like unleaded or diesel fuel?");
scanf("%c", &input);
printf("Enter the litre you want to fuel:");
scanf("%f", &L);
switch (input) {
case 'u' :
cost = 1.98*gallon;
printf("The cost is :%f ",&cost);
break;
case 'd' :
cost = 1.29*gallon;
printf("The cost is :%f ",&cost);
break;
}
getch();
return 0;
}
程序无法显示成本结果,仅在我输入完 scanf 语句和 L 值后才显示成本 = 0.0000。我是c程序的新手,希望能得到帮助。谢谢
【问题讨论】:
-
添加换行符应该可以解决它。否则输出缓冲区不会被刷新。您也可以手动拨打
fflush。 -
使用诸如
float之类的二进制浮点类型来计算货币金额不是一个好主意。货币计算的四舍五入是根据参考十进制表示的精确规则定义的。