【发布时间】:2012-11-12 02:39:22
【问题描述】:
我想获取变量L 的用户输入,但scanf 函数不起作用,如果我尝试输入任何内容,程序将跳转并打印下一个成本语句并退出。
我是C新手,希望能在这里得到一些帮助。谢谢。代码如下:
#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 个字符的数组,但您将其视为 2D 字符数组。