我正在爲我的C編程類編寫卡路里程序,編譯時我沒有得到正確的輸出。例如,我輸入的卡路里數是385,得到的芯片總數超過了我原來輸入的卡路里。我已經包含下面的代碼。程序編譯但並沒有給出正確的值
任何幫助將不勝感激。
#include "stdafx.h"
void calories(int total, int*pizza, int*chips, int*apple, int*mustard)
{
if (total >= 385)
*pizza = (total - 385);
if (total >= 170)
*chips = (total - (*pizza * 385))/170;
if (total >= 80)
*apple = (total - (*pizza * 385) - (*chips * 170))/80;
if (total >= 5)
*mustard = (total - (*pizza * 385) - (*chips * 170) - (*apple * 80))/5;
return;
}
int main(void)
{
int total, pizza, chips, apple, mustard;
printf("Enter the total whole number of calories you would like to eat for your meal: ");
scanf_s("%d", &total);
calories(total, &pizza, &chips, &apple, &mustard);
printf("\n Total= %d", total);
printf("\nSlices of pizza= %d", chips);
printf("\nBags of chips= %d", pizza);
printf("\nSlices of apple= %d",apple);
printf("\nTeaspoons of mustard= %d", mustard);
return 0;
}
代碼做了很多,如果整數算術。也許你想浮點。 IAC,張貼「獲得超過我原來輸入的卡路里的芯片總數」,以及爲什麼你認爲這是不正確的。 – chux
1.檢查'scanf_s'的返回值2.將初始值設置爲零。 –
1)你做了什麼? 2)你預計會發生什麼? 3)發生了什麼?你已經回答1但不是2或3. – immibis