can someone help me read a text file and store the file contents into a 2D array?
Here's the file:
100 101 102 103 104 105
106 107 108 109 110 111
112 113 114 115 116 117
118 119 120 121 122 123
124 125 126 127 128 131
Here's my code:
const int ROWS = 5;
const int COLS = 6;
int array[ROWS][COLS];
ifstream inputFile;
inputFile.open("table.txt");
for (int i = 0; i <= ROWS; i++) {
for (int j = 0; i <= COLS; j++) {
inputFile >> array[i][j];
}
}
}
When i run the program and try and display the array, it doesn't work. Help would be greatly appreciated
As it's set up now, the nested loop is infinite. You should be testing the value of j not i. Once you've got that fixed, you'll need to adjust the conditions again. If you leave them as they currently are you will be accessing outside the bounds of your array.