0
我無法弄清楚爲什麼輸出會經歷兩次。爲什麼打印兩次?
int lines = 3
myReadFile.open("graph.txt");
if (myReadFile.is_open()) {
//Read in each value one at a time
while (!myReadFile.eof()) {
for(int i = 0; i < lines; i++) {
for(int j = 0; j<lines; j++) {
myReadFile >> output;
output2 = atoi(output);
Graph[i][j] = output2;
cout << "Graph[" << i <<"][" << j <<"] = " << output2 << endl;
}
//cout << output << output2 << endl;
}
}
} else {
cout << "graph.txt does not exist." << endl;
}
myReadFile.close();
輸出低於:
Graph[0][0] = 0
Graph[0][1] = 65
Graph[0][2] = 4
Graph[1][0] = 7
Graph[1][1] = 0
Graph[1][2] = 68
Graph[2][0] = 67
Graph[2][1] = 84
Graph[2][2] = 0
Graph[0][0] = 0
Graph[0][1] = 0
Graph[0][2] = 0
Graph[1][0] = 0
Graph[1][1] = 0
Graph[1][2] = 0
Graph[2][0] = 0
Graph[2][1] = 0
Graph[2][2] = 0
它做什麼,我需要它,但它可以追溯到雖然和零點出來。任何幫助將是偉大的! 謝謝!
['while(!eof())'wrong。](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong)考慮事實上你在檢查它是否有效之前使用了輸入。 – chris
你在說什麼?編輯:我點擊鏈接。現在看它 – DDukesterman
這個鏈接解釋得非常好,我想。有什麼具體的你不明白?重點是確保您的輸入在使用前成功*。 – chris