#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程序和希望可以得到幫助。謝謝c-cant printf成本
添加一個換行符可以解決它。否則,輸出緩衝區不會刷新。您也可以手動調用'fflush'。 –
使用二進制浮點類型(如float)計算貨幣金額不是一個好主意。貨幣計算的四捨五入是根據涉及十進制表示的精確規則來定義的。 –