0

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


*/

}
7
  • Are you allowed to use std::vector or std::array? (They make the job a tad easier.) Commented Nov 30, 2018 at 8:58
  • My teacher hasn't mentioned anything about vectors so I'm thinking only arrays Commented Nov 30, 2018 at 9:00
  • For my first attempt, only 1 or 0 would be the output no matter what I changed cout << array1[x]; to. The second attempt was when I tried to follow something I found online but I wasn't sure how to take the information from the file to the arrays after I followed those for loops. Commented Nov 30, 2018 at 9:06
  • Well, you have a line in your text file: Day High(F) Low(F) ... you need to clear that away first, if you haven't done so already (or else your cin's will fail to read numbers). Note that, the for-loop snippet from your assignment description is only an example, you'll need to read in text as well e.g. "jan" Commented Nov 30, 2018 at 9:07
  • In my weather.txt file that I am using, I have removed that line Commented Nov 30, 2018 at 9:10

1 Answer 1

1

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
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.