2014-09-12 78 views
0

我剛剛開始學習如何編寫代碼並決定嘗試創建一個程序,該程序可以基於某些要求的值來計算人們燃燒的卡路里量。然而,每當我運行它而不是基於它們的值獲取計算值時,我總是得到值2686708.無論如何,我似乎無法使它工作。C - 輸出值計算不正確

//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; 
} 
+2

'#define CALORIES_PER_POUND 3500 * /'行不應該有關閉註釋符號。 – 2014-09-12 15:48:25

+3

此外,你printf一個整數,你傳遞一個指針:每天。「,calories_burned); – 2014-09-12 15:49:07

+3

你也需要輸入你的變量*之前*做他們的計算! – 2014-09-12 15:49:47

回答

4

此:

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

打印變量的地址,而不是變量的值(爲int,這又是不確定的行爲,但似乎並不適合你至少炸燬)。

它應該是:

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

刪除該&

+0

這是一個常見的新到C程序員錯誤,因爲您需要'&int_variable'作爲'scanf()',而'int_variable'用於'printf()' - 這是因爲scanf需要改變'int_variable'的值,而printf只需要查看它。 – 2014-09-12 16:50:50