2D array read from file help

I am supposed to build a 3 by 30 2D array from this data, the first column is months 6-8, the second is days 1-30 repeating for each month, the 3rd is weather data for each day:
6 1 C
6 2 S
6 3 R
6 4 R
6 5 C
6 6 S
6 7 S
6 8 S
6 9 S
6 10 S
6 11 C
6 12 S
6 13 C
6 14 S
6 15 S
6 16 S
6 17 C
6 18 C
6 19 R
6 20 C
6 21 S
6 22 C
6 23 S
6 24 S
6 25 S
6 26 S
6 27 S
6 28 C
6 29 S
6 30 R
7 1 R
7 2 S
7 3 S
7 4 R
7 5 R

My instructor told me to use the while loop I have set up to extract the data and said I could build the 3 by 30 array in the while loop somehow using this:

weather[month - adjusment][day - 1] = weatherStatus

I have read my book, searched this site, and I do not understand:
1. how the while loop extracts the data into month, day, and weatherStatus. Does the while loop know to move from month to day to weatherStatus at the coulumn breaks?

2. How do I build the 3 by 30 array like this? There are no examples in my book or notes on any websites I have searched showing this. I do not understand. Please help.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

    int month, day;
    char weatherStatus;
    ifstream inputFile;
    // Open file
    inputFile.open("RainOrShine.dat");

    // If file open, process it.
    if (inputFile)
    {
        while(inputFile >> month >> day >> weatherStatus)

        {
            // do stuff with the data

        }
       
        // Close file
        inputFile.close();
    }
    else
    {    // Display error message.
        cout << "ERROR: cannot find/read file." << endl;
    }
Last edited on
You need to declare a char array 3x30, and adjustment (6 in your case)
1
2
char weather[3][30];
const int adjustment=6;

Then on line 15
weather[month - adjusment][day - 1] = weatherStatus;

1. The command on line 11 reads 2 ints and a char, execute what is between the brackets, then tries to read again 2 ints and a char. The inputFile >> month >> day >> weatherStatus will ignore line endings
Topic archived. No new replies allowed.