【发布时间】:2016-09-15 19:31:40
【问题描述】:
我正在尝试编写一个简单的 C 程序来模拟使用固定价格常量的购物体验,然后要求用户输入他们想要购买的金额。然后将数量和价格相乘以获得总成本。
#define TSHIRT 18.95f
//TSHIRT is a constant float for a fixed price on the cost of a t-shirt
int main(void) {
float numberOfShirts;
printf("How many T-Shirts would you like?");
fflush( stdout);
scanf("%f", &numberOfShirts);
printf("You will receive %f shirt(s)", numberOfShirts);
fflush( stdout);
//This gets the user's amount of shirts they'd like to buy
float totalCost = (numberOfShirts * TSHIRT); //Edit: float totalCostadded
printf("%f", totalCost);
//this is supposed to be the total cost (amount * price) and print it,
//but I get an error -- "incompatible type for argument 1 of 'printf ".
如何修复它或获得正确的类型以使其正常工作?
【问题讨论】:
-
totalCost声明未发布。问题喜欢在它的声明中。试试double totalCost = (numberOfShirts * TSHIRT); -
为什么需要
float来表示衬衫的数量?有人会买半件衬衫吗? -
我会带上我的无袖衣服,@WeatherVane。我猜这大约是 0.6 件衬衫。 :)
-
@Weather Vane 省略换行允许提示和输入出现在同一行。使用
'\n'会改变用户体验。 OP 所做的很好,并建议在提示可能缺少最终的'\n'时使用fflush(stdout)。
标签: c variables floating-point constants