0
我在這裏是新的,所以如果我在提出這個問題時做了任何錯誤,我表示歉意。我在C中,每次從文件中讀取元素的數字時,我都試圖給數組中元素的值添加一個。當它讀取數字30時,我將它設置爲終止。我相信我正在制定出界限錯誤或者沿着這些界限的事情,因爲在我嘗試下面的代碼後打印出的值是瘋狂的。從文本文件分配給數組的錯誤值
int main(){
int votes[20];
FILE *input;
input = fopen("votes.txt", "r");
int currentVote;
while(currentVote != 30){
fscanf(input, " ");
fscanf(input, "%d",¤tVote);
if(currentVote == 30){
break;
}
votes[currentVote] += 1;
}
fclose(input);
int i;
int l = 19;
int x;
for(i = 0; i <= l; i++){
x = i;
printf("%d is %d\n",i,votes[x]);
}
return 0;
}
不幸的是,這是我得到的輸出。
0 is 1
1 is 1
2 is 1
3 is 1
4 is 1835627637
5 is 1600061542
6 is 1869833335
7 is 1952802656
8 is 1
9 is 1
10 is 1
11 is 1
12 is 2
13 is 1
14 is 4196110
15 is 1
16 is 1
17 is 1
18 is 1
19 is 1
這是輸入文本文件:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 30
預先感謝您。
我是個白癡。謝謝! – hunter3035
這是每個人至少犯過一次錯誤。 – GWW