我正在嘗試編寫一個簡單的C程序來模擬使用固定常量的價格的購物體驗,然後要求用戶輸入他們想要的數量採購。然後乘以金額和價格,以獲得總成本。使用浮點常量和變量的'printf'參數1的不兼容類型
#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);' – chux
另外,請閱讀[MCVE] – Serge
爲什麼襯衫的數量需要一個「浮動」?有人會買半件襯衫嗎? –