#include <iostream>;
#include <fstream>
#include <iomanip>;
using namespace std;
int main() {
// Create Text File to store the Values
const int ARRAY_SIZE = 10;
int numbers[ARRAY_SIZE] = { 5, 10, 8, 7, 3, 4, 9, 6, 2, 1 };
int store_count;
ofstream outputFile;
outputFile.open("TenNumbers.txt");
for (store_count = 0; store_count < ARRAY_SIZE; store_count ) {
outputFile << numbers[store_count] << endl;
}
outputFile.close();
int count = 0;
int num_row1[5];
int num_row2[5];
int nums_array[2][5];
ifstream inputFile;
inputFile.open("TenNumbers.txt");
while (count < 5 && inputFile >> num_row1[count])
cout << num_row1[count] << endl;
// Store value into 2d Array
count ;
while (count < 10 && inputFile >> num_row2[count])
cout << num_row2[count] << endl;
// Store value into 2d Array
count ;
inputFile.close();
cout << endl;
return 0;
};
這是它正在讀取的檔案的輸出和產生錯誤的陣列:
這是需要在二維陣列中的檔案編號 5 10 8 7 3 4 9 6 2 1
這是二維陣列顯示的內容 1 -858993460 -858993460 -858993460 -858993460
uj5u.com熱心網友回復:
您的錯誤幾乎肯定存在于這種型別的代碼中。
while (count < 5 && inputFile >> num_row1[count])
cout << num_row1[count] << endl;
// Store value into 2d Array
count ;
如果我們更正確地縮進,我們會得到:
while (count < 5 && inputFile >> num_row1[count])
cout << num_row1[count] << endl;
// Store value into 2d Array
count ;
該count ;陳述句不在 while 回圈中,因此count永遠不會更改。這意味著您永遠不會在陣列中填充多個元素,其余元素未初始化,這意味著它們的內容未定義。
你可能想寫:
while (count < 5 && inputFile >> num_row1[count]) {
cout << num_row1[count] << endl;
// Store value into 2d Array
count ;
}
uj5u.com熱心網友回復:
首先,你不需要;after#include <...>因為#include <...>不是一個宣告。在;之后它只是評價,分為一個空陳述句。
還
while (count < 5 && inputFile >> num_row1[count])
cout << num_row1[count] << endl;
// Store value into 2d Array
count ;
while (count < 10 && inputFile >> num_row2[count])
cout << num_row2[count] << endl;
// Store value into 2d Array
count ;
我想你的意思是:
while (count1 < 5 && inputFile >> num_row1[count1])
{
std::cout << num_row1[count1] << std::endl; // change to different variable names
// Store value into 2d Array
count ;
while (count2 < 10 && inputFile >> num_row2[count])
{
std::cout << num_row2[count2] << std::endl;
// Store value into 2d Array
count ;
} // block scope with proper indentation
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/328464.html
標籤:C
上一篇:運行我的程式時讀取訪問沖突
下一篇:神秘的C 可變引數模板擴展
