C++, Trying to open a file, read it, then store the data by creating an instance of my class

I have no clue how to do this, but I am supposed to open a file, read it, and put the data in an array that is within a class header file through creating an instance of it.

This is the main.cpp
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
  #include <iostream>
#include "WEATHERFORECASTER.h"
#include <fstream>
#include <sstream>
#include <string>
#include <ios>

using namespace std;

int main()
{
    WeatherForecaster();
    ifstream Fore;                          //Open up the file DATABOULDER.csv for weather data
	Fore.open("DATABOULDER.csv");
	if (Fore.fail())                        //If it fails nothing will happen
	{
	}
	else
	{                                           //Otherwise open the file and begin sorting the data
		string weather;                         //Create a string called weather
		int lineIndex = 0;                      //Create a counter for the lines
		while (getline(Fore, weather, '\n'))    //Move through each line of the data by stopping at a character return
		{                                       //Set the weather variable to that whole line of data
			stringstream ss;                    //Create a stream using the weather string
			ss << weather;
			int weatherIndex = 0;               //Create a new Index counter for each piece of data within the line
			while (getline(ss, weather, ','))   //Go through the line and every comma store that as a weatherIndex
			{
				if (weatherIndex == 0)                      //Begin setting the pieces of the array beginning with the first index
				{
					string day = yearData[lineIndex].day;   //If the index is 0 then set it as the .day extension
					yearData[lineIndex].day = weather;      //Set this equal to the weather variable in order to get the actual piece of data
				}
				else if (weatherIndex == 1)                                     //If Index is 1 then this is the forecast day so use that extension
				{
					string forecastDay = yearData[lineIndex].forecastDay;
					yearData[lineIndex].forecastDay = weather;              //Set that equal to the weather variable to get actual data
				}
				else if (weatherIndex == 2)                                     //If the Index is 2 then this is the high temp
				{
					istringstream convert(weather);                             //First convert weather so it can take ints
					int highTemp = 0;                                           //Create a highTemp int variable
					string strHighTemp = "";                                    //Create a string to use with the string for converting the highTemp
					strHighTemp = weather.substr(2, 2);                         //This allows for the H: to be removed and only a number or int
					if (!(istringstream(strHighTemp) >> highTemp)) highTemp = 0;//Converting string highTemp to int highTemp and if it fails set highTemp to 0
					yearData[lineIndex].highTemp = highTemp;
				}
				else if (weatherIndex == 3)
				{
					istringstream convert(weather);                             //Perform the exact same steps as previous for low temperatures
					int lowTemp = 0;
					string strLowTemp = "";
					strLowTemp = weather.substr(2, 2);
					if (!(istringstream(strLowTemp) >> lowTemp)) lowTemp = 0;
					yearData[lineIndex].lowTemp = lowTemp;
				}
				else if (weatherIndex == 4)                                 //If Index is 4 then that is humidity and we need to convert
				{
					istringstream convert(weather);                         //Convert weather to take ints
					int humidity = 0;                                       //Initialize a variable for humidity
					if (!(istringstream(weather) >> humidity)) humidity = 0;//Convert string humidity to int humidity and if it fails set humidity to 0
					yearData[lineIndex].humidity = humidity;            //Set this index of the array to humidity variable type int
				}
				else if (weatherIndex == 5)                                 //If Index is 5 then that is avgWind and we must convert
				{
					istringstream convert(weather);                         //Convert weather to take ints
					int avgWind = 0;                                        //Initialize variable for avgWind
					if (!(istringstream(weather) >> avgWind)) avgWind = 0;  //Convert string avgWind to int avgWind and if it fails set avgWind to 0
					yearData[lineIndex].avgWind = avgWind;              //Set this index of the array to the avgWind variable type int
				}
				else if (weatherIndex == 6)                         //If Index is 6 then it is the avg Wind Direction
				{
					yearData[lineIndex].avgWindDir = weather;   //Set this index of the array to weather since it is a string
				}
				else if (weatherIndex == 7)                         //If Index is 7 then it is max Wind
				{
					istringstream convert(weather);                 //Convert weather to take ints
					int maxWind = 0;                                //Initialize variable for maxWind
					if (!(istringstream(weather) >> maxWind)) maxWind = 0;//Convert string maxWind to int maxWind and if it fails set maxWind to 0
					yearData[lineIndex].maxWind = maxWind;      //Set this index of the array to the maxWind variable type int
				}
				else if (weatherIndex == 8)                         //If Index is 8 then it is max Wind Direction
				{
					yearData[lineIndex].maxWindDir = weather;   //Set equal to weather since it is a string
				}
				else if (weatherIndex == 9)                         //If Index is 9 then it is precipitation
				{
					istringstream convert(weather);                 //Convert weather to take doubles
					double precip = 0;                              //Initialize variable for precipitation type double
					if (!(istringstream(weather) >> precip)) precip = 0;//Convert string precip to int precip and if it fails set it to 0
					yearData[lineIndex].precip = precip;        //Set this index of the array to the precip variable of type double
				}
				weatherIndex++;     //Increment each weatherIndex to get all lines
			}
			lineIndex++;            //Increment each lineIndex to get all pieces from the lines
		}
	}
    WeatherForecaster yD;
    yD.printDaysInData();
    //yD.addDayToData();
//    yD.printForecastForDay("9-16-2016");
    yD.getFirstDayInData();
    yD.getLastDayInData();
}


This is the header file

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#ifndef WEATHERFORECASTER_H
#define WEATHERFORECASTER_H

#include <iostream>


struct ForecastDay{
    std::string day;
    std::string forecastDay;
    int highTemp;
    int lowTemp;
    int humidity;
    int avgWind;
    std::string avgWindDir;
    int maxWind;
    std::string maxWindDir;
    double precip;

};

class WeatherForecaster
{
    public:
        WeatherForecaster();
        ~WeatherForecaster();
        void addDayToData(ForecastDay);
        void printDaysInData(); //prints the unique dates in the data
        void printForecastForDay(std::string);
        void printFourDayForecast(std::string);
        double calculateTotalPrecipitation();
        void printLastDayItRained();
        void printLastDayAboveTemperature(int); //argument is the temperature
        void printTemperatureForecastDifference(std::string);
        void printPredictedVsActualRainfall(int); //argument is days out, such as 1 = 1 day out, 2 = 2 days out, 3 = 3 days out
        std::string getFirstDayInData();
        std::string getLastDayInData();

    protected:
    private:
        int arrayLength = 984;
        int index;
        ForecastDay yearData[984]; //data for each day
};

#endif // WEATHERFORECASTER_H 


Here is the implementation file
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include "WEATHERFORECASTER.h"
#include <iostream>


using namespace std;

WeatherForecaster::WeatherForecaster()
{
    //ctor
}

WeatherForecaster::~WeatherForecaster()
{
    //dtor
}

/*void WeatherForecaster::addDayToData(ForecastDay day)
{
     if(index < arrayLength)
    {
        yearData[index].forecastDay = ForecastDay;
        index++;
    }
    else
    {
        cout<<"array full"<<endl;
    }
}*/

void WeatherForecaster::printDaysInData()
{
    //!display contents of array
    for(int i = 0; i < arrayLength; i++)
    {
        if(yearData[i].day == yearData[i].forecastDay)
        {
            cout<<yearData[i].day<<endl;
        }
    }
}

//void WeatherForecaster::printForecastForDay(string index)
//{
/*    cout<<yearData[index].Day<<endl;
    cout<<yearData[index].highTemp<<endl;
}*/

string WeatherForecaster::getFirstDayInData()   //Get the first day within the data set
{
    cout<<yearData[0].day<<endl;                //First day is at the 0 index
}

string WeatherForecaster::getLastDayInData()    //Get the last day within the data set
{
    cout<<yearData[983].day;
}
Hello jlmccart01,

There are several to many problems some i have not figured out yet. Line 12WeatherForecaster(); is this a forward declaration that would be better positioned above main, or a call to a function that you have not defined or a call to the class constructor that you should not do? I think what you want here is something like this: WeatherForecaster aVriableName[arraySize]. This is where the array should be defined not in the private section of the class where you have no way to get to it.

On line 15 your comment says "If it fails nothing will happen", but something should happen. You should print a message to say there is a problem opening the file, give time to read the message and then exit the program because nothing else should happen until you figure out why the file did not open.

Lines 29 - 91 all refer to yearData[lineIndex].something which you can not do because yearData[] is a private member variable of the class that you can only access through a public member function which you do not have e.g., the addDayToData function that you have commented out or a member function to get the private data. The else if statements that deal with changing a string to an int seem to be a bit over coded for what needs to be done. and the lines like
if (!(istringstream(strHighTemp) >> highTemp)) highTemp = 0; I am not sure if it would even work, I say that not having tested it yet

In the header file for the class lines 40 and 42 should be defined in main not there.

I would guess that there are more problems that I have not found yet, but that should get you started for now.

Hope that helps,

Andy
Topic archived. No new replies allowed.