简体   繁体   中英

C++ .txt values to 2D array

I'm a first semester Computer Science major and I got this assignment last week and I can't figure out how to do it. We barely went over files in class so I'm not really sure how to even attempt this. I'm not asking for my whole assignment to be done for me, I just can't figure out how to put the given txt file into a 2D array. From there I believe I can figure it out.

Basically we are taking this .txt file and taking the weather data and putting it into an array. I'm not sure how to do this with files. I'm honestly stuck and I haven't found anything online that makes sense to me because we've barely gone over this in class.

Thank you in advance! :D

Assignment Description

Here is the weather file (.txt) we were given:

Day High(F) Low(F)  Precip.(inch)   Snow(inch)  SnowDepth(inch)
1 jan 2016  23.0    10.0    0.00    0.00    9.02
2 jan 2016  24.1    -7.1    0.00    0.00    9.02
3 jan 2016  30.0    10.9    0.00    0.00    9.02
4 jan 2016  37.9    28.9    0.01    0.00    7.99
5 jan 2016  41.0    30.9    0.00    0.00    5.98
6 jan 2016  43.0    34.0    0.00    0.00    5.00
7 jan 2016  37.0    28.0    0.00    0.00    5.00
8 jan 2016  35.1    28.9    0.01    0.00    4.02
9 jan 2016  39.0    28.0    0.00    0.00    4.02
10 jan 2016 30.9    23.0    0.00    0.00    4.02
11 jan 2016 30.9    18.0    0.00    0.00    4.02
12 jan 2016 36.0    28.9    0.00    0.00    4.02
13 jan 2016 37.9    32.0    1.30    0.00    2.99
14 jan 2016 39.0    25.0    0.17    0.00    2.99
15 jan 2016 34.0    19.9    0.01    0.00    2.01
16 jan 2016 34.0    26.1    0.24    2.01    2.99
17 jan 2016 37.9    28.9    0.10    0.00    2.99
18 jan 2016 39.9    32.0    0.39    0.00    2.01
19 jan 2016 39.9    30.0    0.03    0.00    2.01
20 jan 2016 41.0    32.0    0.50    0.00    0.98
21 jan 2016 39.0    30.9    0.23    0.00    0.00
22 jan 2016 45.0    35.1    0.13    0.00    0.00
23 jan 2016 44.1    32.0    0.23    0.00    0.00
24 jan 2016 44.1    34.0    0.09    0.00    0.00
25 jan 2016 39.9    28.0    0.00    0.00    0.00
26 jan 2016 39.0    30.0    0.01    0.00    0.00
27 jan 2016 44.1    36.0    0.05    0.00    0.00
28 jan 2016 39.9    36.0    0.67    0.00    0.00
29 jan 2016 39.9    32.0    0.32    0.00    0.00
30 jan 2016 39.0    30.9    0.02    0.00    0.00
31 jan 2016 37.9    28.9    0.01    0.00    0.00

Here are my attempts at this assignment:

 // This program reads data from a file into an array

#include <iostream>
#include <fstream> // To use ifstream
using namespace std;

int main()
{

// My first attempt with the assignment hints

/*
    int x;
    ifstream infile; // Create an ifstream object

        infile.open("weather.txt"); // Open weather file
        infile >> x; // read in a single integer from the file

        cout << x << "\n" << endl; // Test x variable

        int i;
    int array1[x];
        int array2[x];
        int array3[x];

        for(int i = 0; i < 31; i++){
                infile >> array1[i];
                infile >> array2[i];
                infile >> array3[i];
                }

        cout << array1[6];
*/


// Attempt 2:

/*
int table[rows][columns];

        for(int i = 0; i < rows; i++)
        {
                for(int j = 0; j < columns; j++)
                {
                        table[i][j] = 0;

                } // End colums loop

        } // End rows loop


*/

}

Look at this simple example. It's reading your file in table of strings. The first line (with table headers) is ignored.

#include <vector>
#include <string>
#include <sstream>
#include <fstream>
using namespace std;



int main(int argc, char* argv[])
{
    using line = vector<string>; // one line of strings
    using table = vector<line>;  // table is array of lines

    ifstream file("1.txt");
    string s;
    std::getline(file, s); // get header line

    table tbl;

    while(std::getline(file, s)) // getting new line
    {
        stringstream ss(s);
        line ln;
        while(ss >> s)           // parse elements of line
            ln.push_back(s);
        tbl.push_back(ln);       // insert line into table
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM