我想获得变量L
的用户输入,但scanf
函数不起作用,程序将跳转并打印下一个成本语句并退出,如果我尝试输入任何东西。 我是C新手,希望能在这里得到一些帮助。谢谢。下面的代码:C - scanf没有停止用户输入
#include <stdio.h>
#include <conio.h>
int main()
{
float L = 0; // L is litre
float gallon;
gallon = 3.785 * L;
char x[2] = {'u', 'd'}; // u is unleaded and d is diesel
float cost;
printf("Hello, welcome to PetrolUpHere!!\n");
printf("Would u like unleaded or diesel fuel?");
scanf("%s", &x[2]);
printf("Enter the litre you want to fuel:");
scanf("%.2f", &L); //SCANF NOT WORKING
switch (x[2]) {
case 'u':
cost = 1.98 * gallon;
printf("The cost is :%.2f ", cost);
break;
case 'd':
cost = 1.29*gallon;
printf("The cost is :%.2f ",cost);
break;
}
getch();
return 0;
}
x是2个字符的数组,但是您将它视为一个二维数组字符。 – John3136