我在這裏遇到問題。我正在嘗試從用戶處獲得幾個輸入,並根據他工作的天數計算總輸入和平均值。例如,如果他工作了10天,當他輸入10時,程序會要求他輸入10天的工作時間。 iterateArray數組只能保存最多30天和變量天數,並保存輸入的天數。我怎樣才能得到使用scanf的值?使用scanf獲取用戶的幾個輸入
int main(void){
printf("The program calculates the total hours worked during\n");
printf("a specific period and the average length of a day.\n\n");
printf("How many days:");
scanf("%d",&days);
do{
if(i==days){
break;
i++;
}
else{
printf("Enter the working hours for day %d:",++i);
scanf("%f",&iterateArray[0]);
}
}while(i<days);
}
您的代碼並不顯示'i'或'iterateArray'的定義。你還應該檢查'scanf()'操作是否成功,而不是簡單地假定它們工作。你的代碼縮進留下了很多不盡人意的地方。我相信你可以使用'for'循環來代替'do ... while'循環,這會讓你的代碼更容易理解。 – 2014-11-02 06:02:47