简体   繁体   中英

C++ Getting maximum value from CSVwith year and month from user input without STL

I'm trying to read from a CSV file with the following values. dateTime ,DP,Dta,Dts,EV,QFE,QFF,QNH,R,RH, Speed , radiation ,ST,ST2,ST3,ST4,Sx,T

31/03/2016 9:00 14.6 175 17 0 1013.4 1016.9 1017 0 68.2 6 512 22.7 24.1 25.5 26.1 8 20.74 31/03/2016 9:10 14.6 194 22 0.1 1013.4 1016.9 1017 0 67.2 5 565 22.7 24.1 25.5 26.1 8 20.97

Currently the column that i need the data from is from dateTime , speed and radiation. However , when i call on the windlog.print , dateTime is printed every alternate line starting from second line and radiation is printed every alternate line starting from the third line , so i am wondering did i do anything wrong with the ignore function ? Cus i am not too farmiliar with it....

Key problem here is we are not suppose to use any STL data structures. I have written a simple vector class for this.

typedef struct
{
 Date d;
 Time t;
 float speed;
 int solar;
}
W;
//Methods

void getMaxSpeed(int y2, int m2);
//var
int choice, year,m, tt;
ifstream input;
string filename,month,num;
Vector<W> windlog;
W T1;
ostream & operator << (ostream & osObject, const WindLogType & W1);
istream & operator >> (istream & input, WindLogType & W1);
int main()
{
 filename = "test.csv";
 string line2,line,sDay, sMonth, sYear, sHH, sMM;

 input.open(filename.c_str());
 input.ignore(500,'\n');
 if(!input.is_open())
 {
    cout<< "File not found."<<endl;
    return 0;
 }
 else
 {
    while(getline(input,line))
    {
        //Get Date
        getline(input, sDay,'/');
        getline(input, sMonth,'/');
        getline(input, sYear,' ');
        //Get Time
        getline(input, sHH,':');
        getline(input, sMM,',');
        //Parse into variable , convert from string to int
        int day1 = atoi(sDay.c_str());
        int month1 = atoi(sMonth.c_str());
        int year1 = atoi(sYear.c_str());
        int hour1 = atoi(sHH.c_str());
        int min1 = atoi(sMM.c_str());

        float s1; 
        int sr;
        // speed
        for (int i=0; i<10; i++)
        {
            input >> s1;
            input.ignore(100, ',');
        }
        //radiation
        for(int j =0; j<18; j++)
        {
            input >> sr;
            input.ignore(90,',');
        }

        // create a record
        T1.d.setDate(day1, month1, year1);
        T1.t.setTime(hour1, min1);
        T1.speed = s1;
        T1.solar = sr;

        windlog.push_back(T1);         
    }
}
windlog.print();
getMaxSpeed(2017,3);

return 0;
}

I'm trying to get the maximum speed based on the year and month from user input accordingly. But the current output for getMaxSpeed(2017,3) is: 'Maximum Wind Speed for the year 2017 in the 3rd month is : 6.30584e-044' So i need to know what is the proper syntax to filter accordingly . GetMaximumValue method

void getMaxSpeed(int y2, int m2)
{

 float maxSpeed;
 for (int i=0; i<windlog.size(); i++)
 {
    int y3 = windlog.at(i).d.getYear();
    int m3 = windlog.at(i).d.getMonth();
    if(y3==y2 && m3==m2){
    while(y3==y2 && m3==m2)
    {
        maxSpeed = windlog.at(0).speed;
        if (windlog.at(i).speed > maxSpeed)
        {
            maxSpeed = windlog.at(i).speed;
        }
     }

    }
  }
  cout<< maxSpeed << endl;

  }

Once i figure out for the maxspeed i will be able to do the rest of the functions....

From what you've provided, you're telling ignore to look for a comma, but there aren't any in your data, so it eats 90 or 100 characters instead. If you change the delimiter to ' ', you should get the results you want

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