I'm trying to read data and output only selected data from a CSV file(Date, time and S(speed). My program is able to run but it loops in a weird way. Line 1, than Line 1 and 2, than Line 1,2 and 3 and so on and so forth. The final record that is shown in the program is also incorrect(0/3/2016 23:50). Need some guidance to rectify this problem.
Sample data in notepad format below. It contains thousands of line but only pick the last 3 lines to illustrate my problem. Ignore the "Line" below, it's just to tell you which line is in excel format. Line 1 is the header which i ignore, follow by the data.
Line 1:WAST,DP,Dta,Dts,EV,QFE,QFF,QNH,RF,RH,S,SR,ST1,ST2,ST3,ST4,Sx,T
Line 2: 31/03/2016 23:30,11.4,257,11,0,1012.9,1016.4,1016.5,0,63.5,5,13,26.4,25.8,25.6,26.1,7,18.48
Line 3: 31/03/2016 23:40,11.4,250,15,0,1012.9,1016.4,1016.5,0,64.7,5,10,26.4,25.8,25.6,26.1,6,18.19
Line 4:31/03/2016 23:50,11.4,243,5,0,1012.7,1016.2,1016.3,0,65.8,3,11,26.3,25.8,25.6,26.1,5,17.95
Current Output:
31/3/2016 23:30 Speed: 5
31/3/2016 23:30 Speed: 5
31/3/2016 23:40 Speed: 5
31/3/2016 23:30 Speed: 5
31/3/2016 23:40 Speed: 5
31/3/2016 23:50 Speed: 3
31/3/2016 23:30 Speed: 5
31/3/2016 23:40 Speed: 5
31/3/2016 23:50 Speed: 3
0/3/2016 23:50 Speed: 3
Max Speed: 5
Done!
Intended Output
31/3/2016 23:30 Speed: 5
31/3/2016 23:40 Speed: 5
31/3/2016 23:50 Speed: 3
Max Speed: 5
Done!
using namespace std;
typedef struct
{
Date d;
Time t;
float speed;
}
WindLogType;
//declare speed max function
ostream & operator <<(ostream &osObject, const WindLogType & w1);
istream & operator >>(istream &input, WindLogType &w1);
int main()
{
string filename;
ifstream input;
filename = "Data.csv";
input.open(filename.c_str());
input.ignore(500,'\n');
Vector<WindLogType> windlog;
string line,line2, readDay, readMonth, readYear, readHour, readMinute;
float sp;
while(!input.eof())
{
getline(input,readDay,'/');
getline(input,readMonth,'/');
getline(input,readYear,' ');
getline(input,readHour,':');
getline(input,readMinute,',');
int day1 =atoi(readDay.c_str());
int month1=atoi(readMonth.c_str());
int year1=atoi(readYear.c_str());
int hour1=atoi(readHour.c_str());
int minute1=atoi(readMinute.c_str());
float s1;
for(int i = 0;i<10;i++)
{
input>>s1;
input.ignore(50,',');
}
WindLogType T1;//create a record
T1.d.setDate(day1,month1,year1);
T1.t.setTime(hour1,minute1);
T1.speed = s1;
windlog.push_back(T1);//push inside vector
windlog.print();
getline(input,line2,'\n');
}
float maxSpeed;
WindLogType H1;
H1=windlog.at(0);
maxSpeed=H1.speed;
for(int i=0;i<windlog.size();i++)
{
if(windlog.at(i).speed>maxSpeed)
{
maxSpeed=windlog.at(i).speed;
}
}
cout<<"Max Speed: "<<maxSpeed<<endl;
cout<<"Done!"<<endl;
return 0;
}
ostream & operator <<(ostream &osObject, const WindLogType &w1)
{
osObject<<w1.d<<w1.t<<endl<<"Speed: "<<w1.speed;
return osObject;
}
istream &operator >>(istream & input, WindLogType &w1)
{
input>>w1.d>>w1.t>>w1.speed;
return input;
}