gpt4 book ai didi

C - 输出值计算不正确

转载 作者:太空宇宙 更新时间:2023-11-04 06:26:53 25 4
gpt4 key购买 nike

我刚开始学习如何编码,并决定尝试创建一个程序,根据要求的某些值计算一个人燃烧的卡路里量。但是,每当我运行它而不是根据它们的值获取计算值时,我总是得到值 2686708。无论如何我似乎都无法让它工作。

//included libraries
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

//constants
#define WALKING_CAL 5
#define EXERCISING_CAL 10
#define EATING_CAL 40
#define DRINKING_CAL 20
#define CALORIES_PER_POUND 3500*/

//main function
int main() {

int current_weight, goal_weight;
int walking, exercise, drinking, eating;
int total_walking, total_exercising, total_eating, total_drinking;
int calories_burned, calories_gained;

printf("What is your current weight?\n");
scanf("%d", &current_weight);

printf("\nWhat is your goal weight?\n");
scanf("%d", &goal_weight);

total_walking = WALKING_CAL * walking;
total_exercising = EXERCISING_CAL * exercise;
total_eating = EATING_CAL * eating;
total_drinking = DRINKING_CAL * drinking;
calories_burned = (total_walking + total_exercising)- (total_eating + total_drinking);


if (goal_weight > current_weight){
printf("\nHow many minutes do you walk per day?\n");
scanf("%d", &walking);

printf("\nHow many minutes do you exercise per day?\n");
scanf("%d", &exercise);

printf("\nHow many minutes do you drink per day?\n");
scanf("%d", &drinking);

printf("\nHow many minutes do you eat per day?\n");
scanf("%d", &eating);


printf("You gain %d calories per day.", &calories_burned);

}
return 0;
}

最佳答案

这个:

printf("You gain %d calories per day.", &calories_burned);

打印变量的地址,而不是变量的值(作为int,这又是未定义的行为,但至少对你来说似乎不会爆炸) .

应该是:

printf("You gain %d calories per day.", calories_burned);

删除 &

关于C - 输出值计算不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25812085/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com