简体   繁体   English

c ++读取.csv文件,将数据放入变量,然后放入对象

[英]c++ read .csv file, place data into variables and then into an object

hi reached a point where im not sure how to proceed. 您好,我不确定该如何进行。 the main objective of my program: 我程序的主要目标是:

1) read 5 values from a file(.csv) 1)从文件(.csv)读取5个值

  • date (class with 3 variables - day,month, year) 日期(具有3个变量的类-日,月,年)

  • time (class with 2 variables - minute, hour) 时间(带有2个变量的类-分钟,小时)

  • float wSpeed (under class weather) 浮动wSpeed(在课堂天气下)

  • float temperature (under class weather) 浮点温度(根据天气情况)

  • float solarRadiation (under class weather) 浮动solarRadiation(在类天气下)

2) put into an object 2)放入物体

3) put it into a vector 3)将其放入向量

main 主要

   int main()
{
 string filename;
 ifstream input;

Vector<weather> windlog; //have a vector called windlog

cout <<"enter file name:" <<endl;
cin >> filename;

input.open(filename.c_str());
input.ignore(500,'\n');

string sDay, sMonth, sYear, sHH, sMM, wind, solar, temperature;
date d1;
time t1;
weather w1;

getline(input, sDay,'/'); //stop getting input at '/'
getline(input, sMonth,'/');
getline(input, sYear,' ');
getline(input, sHH,':');
getline(input, sMM,',');

int day1 = atoi(sDay.c_str()); //convert string to int (atoi)
int month1 = atoi(sMonth.c_str());
int year1 = atoi(sYear.c_str());
int hour1 = atoi(sHH.c_str());
int min1 = atoi(sMM.c_str());

d1.setDate(day1,month1,year1); //set date using converted string
t1.setTime(min1, hour1); //set time using converted string

         // skip the first 9 columns of .csv file
    for(int i=0; i<9; i++) 
     {
        input.ignore(50, ','); //ignore ','
     }

        //location now at wSpeed date of .csv file
    getline(input, wind,',');
    float wS1 = atof(wind.c_str()); // convert string to float

       //next location is the location solarRadiation
    getline(input, solar,',');
    float sR1= atof(solar.c_str()); // convert string to float

      //move 5 columns
     for(int i=0; i< 5; i++)
     {
      input.ignore(50, ',');
     }

      //at location of temperature
     getline(input, temperature,'\n');
    float temperature1 = atof(temperature.c_str()); // convert string to 
                                                       float

    //when i print it out, it gives me the correct data
    /*
cout << d1; //date class that contains dd,mm,yy
cout << t1;//time class that contains hh, mm
cout << wS1 ;
cout << sR1;
cout << temperature1 << endl;
    */

 //trying to put these data into an object file: weather

 //i tried doing something like this
weather obj1(wS1, sR1, temperature1, d1, t1);
cout << objt1;//gives me weird values but when i cout each variable, it 
 works out fine

not going to write the whole date/time.h/cpp cause i think it'll take up too much space 不会写整个date / time.h / cpp,因为我认为这会占用太多空间

date.h date.h

  public:
  setday, setmonth, setyear, setdate(day,month,year);
  getday,getmonth,getyear;
  private: day,month,year;

time.h 时间

  public:
  setminute, sethour, settime(minute,hour);
  getminute,get hour;
  private: minute, hour;

weather class(where im having the problems) 天气课(我遇到问题的地方)

.H 。H

  #ifndef H_WEATHER  
  #define  H_WEATHER
  #include <iostream>
  #include <string>
  #include "time.h"
  #include "date.h"
  using namespace std;

  class weather: public date, time
     {
     friend ostream& operator << (ostream&, const weather&);
     friend istream& operator >> (istream&, weather&);
        public:
          weather();
          weather(float wSpeed, float solarRadiation, float temperature, 
          date d1, time t1);

          ~weather();

           void setWspeed(float wSpeed);
           void setSolarRadiation(float solarRadiation);
           void setTemperature(float temperature);

           float getWspeed() const ;
           float getSolarRadiation() const;
           float getTemperature() const;
           void setWeather(float wS, float sR, float t,date d1, time t1);
           date getDate();
           time getTime();
           date d1;//mm, hh
           time t1;//dd,mm,yy
        private:
           float wSpeed;
           float solarRadiation;
           float temperature;
           };
      #endif

.CPP .CPP

   #include <iostream>
   #include "weather.h"
   #include "date.h"
   #include "time.h"

    weather::weather()
    {
     wSpeed=0;
     solarRadiation=0;
     temperature = 0;
     }

     weather::weather(float wS, float sR, float t, date d1, time 
     t1):date(day,month,year), time(hours,minute)
      {

         wS = wS;
         sR = sR;
         t =t;
         d1.setDate(day,month, year);
         t1.setTime(hours,minute);
       }
       weather::~weather() {}

       void weather::setWeather(float wS, float sR, float t)
      {
        wSpeed =wS;
        solarRadiation=sR;
        temperature =t;
       }
      void weather::setWspeed(float wS)
       {
        wSpeed =wS;
       }
      void weather::setSolarRadiation(float sR)
      {
       solarRadiation=sR;
      }

      void weather::setTemperature(float t)
      {
       temperature = t;
       }
       void weather::setWeather(float wS,float sR, float t, date d1, time 
       t1)
       {
         wSpeed=wS;
         solarRadiation=sR;
         temperature = t;
       }
       float weather::getWspeed() const
       {
       return wSpeed;
       }
       float weather::getSolarRadiation() const
       {
        return solarRadiation;
       }
       float weather::getTemperature() const
        {
        return temperature;
        }



         ostream& operator<< (ostream& osObject, const weather& weather1)
       {

        osObject << weather1.wSpeed <<"  " << weather1.solarRadiation <<""
        << weather1.temperature <<  weather1.d1 << weather1.t1 ;
        return osObject;
       }
       istream& operator >> (istream& isObject, weather& weather1)
       {
       isObject >> weather1.wSpeed>> weather1.solarRadiation >> 
       weather1.temperature >>  weather1.d1 >> weather1.t1;
       return isObject;
        }

how do i put the values into an object? 如何将值放入对象? is it correct i have to use inheritance so i can overload the weather constructor so it can take a date and time class? 我必须使用继承是否正确,以便可以重载天气构造函数,以便可以采用日期和时间类?

You almost have it, but... 您几乎拥有它,但是...

You do NOT have to inherit to be able to work with a member object. 您不必继承即可使用成员对象。 In

class weather: public date, time

: public date, time is not necessary. : public date, time不是必需的。 It also implies the weather is a date and a time which makes no sense. 它还暗示天气是没有意义的日期和时间。 Avoid inheriting unless there is some logical is-a relationship . 除非存在某些逻辑上的is-a关系,否则请避免继承。

weather::weather(float wS, float sR, float t, date d1, time  t1):
    date(day,month,year), // you want the member name now, not the type 
                          // plus where did day month and year come from?
    time(hours,minute) // not ditto, but close enough.
{
     wS = wS; // self assigns. In other word, does nothing
     sR = sR; // ditto
     t =t; // ditto
     d1.setDate(day,month, year); // where did day month and year come from?
     t1.setTime(hours,minute); // not ditto, but close enough.
}

Doesn't really do anything. 真的什么也没做。 It mostly has the parameters assigning to themselves. 它主要具有为其分配的参数。 This is pointless because they are the same variable and they are temporary variables. 这是没有意义的,因为它们是相同的变量,并且是临时变量。 What you want to do is assign to the member variables. 您要做的就是分配给成员变量。 This is a constructor, so you might as well use the Member Initializer List all the way through 这是一个构造函数,因此您不妨一直使用成员初始化程序列表

weather::weather(float wS,
                 float sR,
                 float t,
                 date d1,
                 time t1) :
        wSpeed(wS), 
        solarRadiation(sR), 
        temperature(t), 
        d1(d1), // note this is about the only time you can use the same name twice. 
                // Enjoy it. But don't do it. It confuses people.
        t1(t1)  // ditto
{
}

Then we get down to setWeather 然后我们开始设置setWeather

void weather::setWeather(float wS, float sR, float t)
{
    wSpeed =wS;
    solarRadiation=sR;
    temperature =t;
}

Does not match the definition in the class 与类中的定义不匹配

void setWeather(float wS, float sR, float t,date d1, time t1);

So obviously we want something more like 所以显然我们想要更多类似的东西

void weather::setWeather(float wS, float sR, float t, date pd1, time pt1)
{
    wSpeed = wS;
    solarRadiation = sR;
    temperature = t;
    d1 = pd1;
    t1 = pt1;
}

Note I did not repeat d1 in the parameter list. 注意,我没有在参数列表中重复d1 I changed it so I wouldn't have d1 = d1; 我改变了它,所以我不会有d1 = d1; which does nothing useful. 这没什么用。 It would be better to give both the member and the parameter more descriptive names. 最好给成员和参数更多的描述性名称。 d1 conveys zero information about that the variable is and how it should be used. d1传达有关该变量为零以及应如何使用的零信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM