I know this question has been asked before, but I cannot seem to find an answer to my particular problem. I am reading in txt file.
weather.txt
1 52 32
2 54 32
3 54 30
4 48 28
5 37 25
6 37 25
7 46 34
8 55 45
9 59 46
10 61 37
11 55 32
12 59 34
There is more data but for space sake I put this.
I am trying to read into the array 0,0 1,0 2,0 then it will go to 0,1 1,1 2,1 then 0,2 1,2 2,2. Just repeating the rows. With the code I have now it just gets stuck. and the output looks like this....
Location: 0 : 0 Data in textFile: 1 Location: 1 : 0 Data in textFile: 1 Location: 2 : 0 Data in textFile: 1 Location: 0 : 1 Data in textFile: 1 Location: 1 : 1 Data in textFile: 1 Location: 2 : 1 Data in textFile: 1 Location: 0 : 2 Data in textFile: 1 Location: 1 : 2 Data in textFile: 1 Location: 2 : 2 Data in textFile: 1 Location: 0 : 3 Data in textFile: 1 Location: 1 : 3 Data in textFile: 1 Location: 2 : 3 Data in textFile: 1 Location: 0 : 4 Data in textFile: 1 Location: 1 : 4 Data in textFile: 1 Location: 2 : 4 Data in textFile: 1 Location: 0 : 5 Data in textFile: 1 Location: 1 : 5 Data in textFile: 1 Location: 2 : 5 Data in textFile: 1 Location: 0 : 6 Data in textFile: 1 Location: 1 : 6 Data in textFile: 1 Location: 2 : 6 Data in textFile: 1 Location: 0 : 7 Data in textFile: 1 Location: 1 : 7 Data in textFile: 1 Location: 2 : 7 Data in textFile: 1 Location: 0 : 8 Data in textFile: 1 Location: 1 : 8 Data in textFile: 1 Location: 2 : 8 Data in textFile: 1 Location: 0 : 9 Data in textFile: 1 Location: 1 : 9 Data in textFile: 1 Location: 2 : 9 Data in textFile: 1 Location: 0 : 10 Data in textFile: 1 Location: 1 : 10 Data in textFile: 1
It goes through the whole file but restarting back to 0,0.
Location: 0 : 0 Data in textFile: 52
Location: 1 : 0 Data in textFile: 52
Location: 2 : 0 Data in textFile: 52
Any help would be appreciated sorry for all the text just trying to be as clear as possible.
#include <iostream>
#include <fstream>
using namespace std;
int main() {
int width = 31;//declaring days or columns for array
int height = 3;//declaring information day and high and low
int data;
/* Code to read in txt file */
ifstream infile;
infile.open("weather.txt");
if (!infile) {
cerr << "Unable to open file C\n";
exit(1); // call system to stop
}
/* end code read text file */
int tempDay[height][width];
//PROBLEM WITH LOOP//
while (infile >> data) {
for (int i = 0; i < width; ++i) {
for (int j = 0; j < height ; ++j) {
tempDay[j][i] = data;
cout << "Location: " << j <<" : " << i << " Data in textFile: " <<data<<endl;
}
}
}
infile.close();
return 0;
}
whileand the twofors.. Think about this and fix it.