输出应该是小的十进制值。我已经完成了它似乎是正确的,但输出仍然是零。例如,如果我分别输入120,230,20和15,它应吐出0.017,但我仍然为零。
#include <stdio.h>
#define WALK_CAL 5
#define STAND_CAL 2
#define DRINK_CAL 20
#define FOOD_CAL 40
#define CALS_PER_POUND 3500
int main(void) {
int walking_min, standing_min;
int drinking_min, eating_min;
float end_calories;
//Get User Input
printf("How long were you walking (in minutes) ?\n");
scanf("%d", &walking_min);
printf("How long were you standing (in minutes) ?\n");
scanf("%d", &standing_min);
printf("How long were you drinking (in minutes) ?\n");
scanf("%d", &drinking_min);
printf("How long were you eating (in minutes) ?\n");
scanf("%d", &eating_min);
//Evaluate User Input
end_calories = ( ( (WALK_CAL*walking_min) + (STAND_CAL*standing_min) - (DRINK_CAL*drinking_min) - (FOOD_CAL*eating_min) ) / CALS_PER_POUND );
//Output Final Value
printf("You lost %.3f pounds today!\n", end_calories);
return 0;
}
提前致谢!
答案 0 :(得分:3)
您看到的是整数截断。最终卡路里数量的小数部分被切断。解决这个问题的一种方法是除以浮点数,即除以:
(CALS_PER_POUND * 1.0)
或者,如果您不介意更改常量,可以使用:
#define CALS_PER_POUND 3500.0f