我已經創建了一個程序,它爲用戶提供一個輸入,然後讓他們輸入他們想要給這個輸入的數量,但是當我打印總數和金額時。然而,這是全線returing的值相同:即浮點輸出不正確
總計:5.00 食品:5.00 賬單:5.00 行程:5.00 的fags:5.00
代替:
總計:14.00 食品:2.00 賬單:3.00 行程:4.00 的fags:5.00
int main(void)
{
float food = 0.00;
float travel = 0.00;
float bills = 0.00;
float fags = 0.00;
float total = 0.00;
float t_food, t_travel, t_bills, t_fags;
char userInput[3];
while(userInput[0] != 'X')
{
printf("Select option,\nA: Food\nB: Travel\nC: Bills\nD: Fags\nX: Exit\n");
scanf("%s", userInput);
if((userInput[0] == 'A') || (userInput[0] =='a'))
{
printf("Please enter an amount: ");
scanf("%f", &food);
printf("You have entered: %.2f\n", food);
t_food += food;
}
if((userInput[0] == 'B') || (userInput[0] =='b'))
{
printf("Please enter an amount: ");
scanf("%f", &travel);
printf("You have entered: %.2f\n", travel);
t_travel += travel;
}
if((userInput[0] == 'C') || (userInput[0] =='c'))
{
printf("Please enter an amount: ");
scanf("%f", &bills);
printf("You have entered: %.2f\n", bills);
t_bills += bills;
}
if((userInput[0] == 'D') || (userInput[0] =='d'))
{
printf("Please enter an amount: ");
scanf("%f", &fags);
printf("You have entered: %.2f\n", fags);
t_fags += fags;
}
if((userInput[0] =='X') || (userInput[0] =='x'))
{
total = t_food + t_fags + t_travel + t_bills;
printf("Total: %.2f\n", &total);
printf("Food: %.2f\n", &t_food);
printf("Travel: %.2f\n", &t_travel);
printf("Bills: %.2f\n", &t_bills);
printf("Fags: %.2f\n", &t_fags);
break;
}
}
return 0;
}
任何想法?
也許這個分解有點孤立,因爲它是寫我要關閉它,因爲它relys用戶輸入...所以隔離您的問題要麼邏輯,輸入或輸出。 – 2013-03-16 11:33:55
大聲笑,哦,是的,我錯過了printpers中的&符號...仍然不會發布沒有reducibg問題的問題 – 2013-03-16 11:36:39