Separate data by storing in array/vector

So I have a example text file which contains weather stats.
Example of text file:
2012-01-01 Rain 7 13 0.28
2012-01-02 ScatteredClouds 4 8 0.25
2012-01-03 Rain 6 12 0.28
2012-01-04 Rain 5 10 0.28
2012-01-05 Rain 7 12 0.28 ... //they are a plenty of list more

How can I read them by putting each data into different type? I already done for the input/output for the text file but I can't figure out how can I arrange my data, I tested a few ways but it still didn't work.

Below is the load code for my cpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void load() { 
char file_name[100]; 
system("cls");
cout<<"Enter data file: ";
cin>>file_name;

ifstream in_stream(file_name);
if(in_stream.is_open())
{
    while(in_stream.good())
    {

    }

}
else if(in_stream.fail())
{
    cout<<"Input file opening failed.\n";
}
}
 


I tested out with in_stream>>X>>Y>>Z something but it still didn't work, need help here!
Thank you guys.
Last edited on
Well, it depends on what you want to do with the numbers.

For the dates, use a string. Same for the word. As for the number. Depends on how you want to read them in, as a string? or as double?

This is an example of how I would do it if the numbers did not exist, and the file only consisted of dates and weather type.

2012-01-01 Rain
2012-01-02 ScatteredClouds
2012-01-03 Rain


1
2
3
4
5
6
7
8
9
10
std::vector<std::string> datesVec;
std::string dates;
std::vector<std::string> weatherVec;
std::string weather;

while (in_stream >> dates >> weather) 
{
	datesVec.push_back(dates);
	weatherVec.push_back(weather);
}


Edit: Fixed from cin >> to in_stream >>
Last edited on
Here is a simple sample how to read the data:
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
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>

using namespace std;

const char tab = '\t';
const char nl = '\n';

int main() 
{
  ifstream src("weather.txt");

  if(!src)
  {
    cerr << "\aError opening file\n";
    system("pause");
    return EXIT_FAILURE;
  }
  string date, condition;
  int num1, num2;
  float num3;
  while(src)
  {
    src >> date >> condition >> num1 >> num2 >> num3;
    cout << date << tab << condition << tab;
    cout << num1 << tab << num2 << tab << num3 << tab<< nl;
  }

  system("pause");
  return EXIT_SUCCESS;
}


BTW. I would create a struct or class to hold the data and also would give them meaningful names.
I knew it could be done with structures and classes, but I am a bit confuse for the private and public part. Which item should I put it in private/public? For my data, there are "Date, Weather, Min. Temperature, Max. Temperature, Rainfall (mm)".
I testing out one of the method with this code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
    ifstream in_stream(file_name);
    if(in_stream.is_open())
    {
        int mintemp,maxtemp;
        string date,weather;
        double rainfall;

        while(in_stream.good())
        {
            in_stream>>date>>weather>>mintemp>>maxtemp>>rainfall;
        }

        cout<<weather;
    }


But it only saves the last line of data, I knew its the problem for defining variables, how can I modified this?
But it only saves the last line of data,

Well yes, that's because you keep overwriting them. That is why you need a vector or a struct or anything to store them in. Like I do in my example.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
    if(in_stream.is_open())
    {
        int mintemp,maxtemp;
        string date,weather;
        double rainfall;

        vector<string>dateVec;

        while(cin>>date)
        {
            dateVec.push_back(date);
        }

        cout<<date;

    }


I edited it by using vector, I test it for date first but my compiler freezes when I try to compile it. May I know why?
Which item should I put it in private/public? For my data, there are "Date, Weather, Min. Temperature, Max. Temperature, Rainfall (mm)".

At this stage in your learning, I'd suggest put everything in the public part. You can learn about the other possibilities later.
Ah shit, my bad I had a mistake in my code, I fixed it there now. It's not freezing, it's waiting for your input because you're using cin >>. You want to use the file instead.


1
2
3
4
while(in_stream >> date) // note in_stream instead of cin >>
{
   dateVec.push_back(date);
}
@TarikNeaj

Thank you so much! It works like a charm... Ermm another question, so if I would like to show how many entries I loaded I can use xxx.size(); right?
Thank you so much! It works like a charm... Ermm another question, so if I would like to show how many entries I loaded I can use xxx.size(); right?

Yes. That'll tell you the size of the vector, which is how many entries it loaded.
So I am done for this part, including moving my variables to classes and make my program become more neatly. =D

For another part of this program, I need to sum up the data I stored in classes and rearrange it.

Example: User input year "2002", the program will auto calculate the part that are belongs to year "2002" and average of minimum/maximum temperate, total rainfall, and also count how many type of weather in the data such as :

Cloudy X(number)
Rain Y(number)

The confusion part is, how do I compare the data inside vector and when users input year the program will only calculate for certain year?
I had done some part of it which read the year that user input and it can count they are total how many days in 1 year in the text file.

This is for the class part:

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
class Weather
{
private:

        int mintemp,maxtemp;
        string date,weather;
        double rainfall;

public:
    Weather(string date, string weather, int mintemp, int maxtemp, double rainfall):date(date),weather(weather),mintemp(mintemp),maxtemp(maxtemp),rainfall(rainfall){}
    
    string getdate()
    {
        return date;
    }
    
    string getyear()
    {
        string abc="9999";
        abc.at(0)=date.at(0);
        abc.at(1)=date.at(1);
        abc.at(2)=date.at(2);
        abc.at(3)=date.at(3);
        return abc;
    }
  
};


This is the code which reads user inputs(year):
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
void stats(vector<Weather>&WeatherSG)
{
    int a,b;
    a=0;
    b=0;
    string year, year2="9999";

    cout<<"Enter year (2012-2015): ";
    cin>>year;
    year2=year;
    year2.at(3)=year2.at(3)-'1'+'0';


    for(int i=0;i<WeatherSG.size();i++)
    {
        if(WeatherSG[i].getyear()== year)
        {
            a=i+1;
        }
        else if(WeatherSG[i].getyear() == year2) //<<need to fix
        {
            b=i+1;
        }
    }

   cout<<a-b<<endl; //not working
}


It is working correctly, but how do I continue for calculating the data inside ?
Last edited on
Topic archived. No new replies allowed.