2014-06-24 46 views
0

問題是我無法將值保存到數組的索引點中。當我嘗試打印二維數組時,它以正確的格式打印,但我只是得到瘋狂的數字而不是文本文件中的數字。這導致我相信我的函數來填充陣列不能正常工作,但我在診斷問題的位置時遇到了問題。嘗試讀取.txt文件中的整數並將它們存儲在二維數組中

這裏是我的函數來填充數組。我的功能是打印數組。

void print2dArray(const int array[][4], int numberUsed) 
{ 
int index1, index2; 

for (index1 = 0; index1 < numberUsed; index1 = index1 + 1) 
{ 
    for (index2 = 0; index2 < 4; index2 = index2 + 1) 
     cout << array[index1][index2] << " "; 

    cout << endl; 
} 
} 

這裏是全局/原型

const int NOP = 25; //Max number of players in the records file 

void fill2dArray(int array[][4], int size, int& numberUsed); 
void print2dArray(const int array[][4], int numberUsed); 

這裏是主

int main() 
{ 
int records[NOP][4], numberUsed; 

fill2dArray(records, NOP, numberUsed); 

print2dArray(records, numberUsed); 


return 0; 
} 

和文本文件(存儲在相同的文件夾程序)

1 2 1 1 
2 3 2 3 
3 2 3 1 
4 0 3 4 
5 2 1 3 
6 5 2 5 
7 2 4 2 
8 4 1 0 
9 0 2 3 
10 1 4 3 
11 2 3 1 
12 3 5 6 
13 2 3 5 
14 2 1 0 
15 2 1 4 
16 7 3 5 
17 9 3 2 
18 6 2 1 
19 3 2 0 
20 1 0 0 
21 0 0 0 
22 1 2 5 
23 2 4 2 
24 6 2 7 
25 6 2 4 
+0

這是使用調試器的典型案例。 –

+0

你能詳細說明一下嗎? – Victor

+0

另外'while((index1

回答

1

01年初,你定義了index1,但是不給它賦值,所以它沒有定義,它可能小於size或大於size

只需將0賦值給index1變量即可。

+0

我試過了,但仍然在輸出中獲得未初始化的值。我已將函數重寫爲手動將值輸入到數組中的位置,並且它將打印正常。這使我相信我用來測試的文本文件存在問題。該文件位於我的桌面的一個文件夾中,與該程序位於同一位置。我將嘗試查看.txt文件本身中是否有任何奇怪的事情發生。 – Victor

+0

@Victor我跑你的代碼,它爲我工作,所以隨時問更多,如果你找到新的東西 – prajmus

+0

我想出了問題。該文本文件被命名爲data11.txt ...我猜程序讀取它爲data11.txt.txt。嘆息..感謝大家的幫助! – Victor

相關問題