My project for my C++ class requires that I take a bunch of weather data. It's a ton of lines so I won't include all of the text file, but here's a sample of it:
0 943
0 5
0 107
2 401
0 299
The first column is snowfall and the second is total precipitation. I have to find things like mean and median with the data, but that's not an issue as I can do math fine. I just don't know how to input all of that data into an array. I was sick and missed class when they went over this so I'm compeltely lost. Basically what I need is help with
1. creating an array that can intake a ton of lines
2. taking the lines from the text file into an array
Thanks!

You're going to want to open a stream to the file, probably with an ifstream, and then read it line-by-line. The ifstream has several extraction methods, but it may be easier to use the getline function, which reads entire lines at a time (see here).

You will probably have some sort of loop for reading the lines. With each line, you'll probably want to split it into a pair of ints. From there, you can add it to a list or vector.

This little example of simple file input from a well ordered (valid) file (of all valid data) ...

// getFileWeatherData //

/*
    My project for my C++ class requires that I take a bunch of weather data.
    It's a ton of lines so I won't include all of the text file,
    but here's a sample of it:

    0 943
    0 5
    0 107
    2 401
    0 299

    The first column is snowfall and the second is total precipitation.
    I have to find things like mean and median with the data,
    but that's not an issue as I can do math fine.
    I just don't know how to input all of that data into an array.
    I was sick and missed class when they went over this so I'm compeltely lost.
    Basically what I need is help with:
    1. creating an array that can intake a ton of lines
    2. taking the lines from the text file into an array
*/

#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

const char* FILE_IN  = "precipitation.txt";

struct Precipitation
{
    int snow;
    int rain;
};




int main()
{
    ifstream fin( FILE_IN );

    if( fin )
    {
        vector< Precipitation > myData;
        Precipitation tmp;

        while( fin >> tmp.snow >> tmp.rain ) // will read till end of file
        {
            myData.push_back( tmp );
        }
        fin.close();

        // show contents of vector ... //
        for( size_t i = 0; i < myData.size(); ++ i )
            cout << myData[i].snow << ", " << myData[i].rain << endl;

    }
    else
    {
        cout << "There was a problem opening file " << FILE_IN  << endl;
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.