我需要询问三个值,但是当我尝试打印问题以询问值时,它会将它们打印在一起
#include <stdio.h>
int main(void)
{
char carModel;
int tankRange;
float gasCost;
float costTank;
printf("What is the model of car? " );
scanf("%c\n", &carModel);
printf("How many miles can be driven on a full tank? " );
scanf("%d\n", &tankRange);
printf("What is the gas cost per gallon? " );
scanf("%f\n", &gasCost);
return 0;
}
我希望得到的是首先询问模型的输出,然后在收到要求完整油箱范围的输出后,最后询问汽油成本然而发生的事情是,它首先要求模型但是到了时候要求另外两个它只是一次打印。我该如何解决这个问题?
删除尾随“ “在您的提示上。您可能还希望 charModel 超过 1 个字符。
#include <stdio.h>
#define CARMODELLEN 20
#define str(s) str2(s)
#define str2(s) #s
int main(void) {
printf("What is the model of car? " );
char carModel[CARMODELLEN+1];
scanf("%" str(CARMODELLEN) "s", &carModel);
printf("How many miles can be driven on a full tank? " );
int tankRange;
scanf("%d", &tankRange);
printf("What is the gas cost per gallon? " );
float gasCost;
scanf("%f", &gasCost);
}