简体   繁体   中英

Access struct variable by its name

im trying to create program which will work with data provided in CSV file.

So far, i've made a structure of this CSV file and able to populate vector with its data.

What im trying to achieve is to allow user to enter Sensor Name and check it with given list of Sensors.

What i want to do is to after user have entered Sensors Name, access already created vector with data and display data of that particular sensor.

Here is the structure i've made:

typedef struct SensorReadings {
    int Month;
    int Day;
    double Dp;                  // Dew Point Temperature
    double Dta;                 // Wind Direction (10min Avg)
    double Dts;                 // Wind Direction (10min SD)
    double EV;                  // Evaporation (Year to Date)
    double QFE;                 // Barometric Pressure
    double QFF;                 // Barometric Pressure (QFE*Kqff)
    double QNH;                 // Barometric Pressure (Aviation Standard)
    double RF;                  // Rainfall (Year to Date)
} SensorReadings;

And here is the code, where i prompt user to enter Sensor Name:

std::cout << std::endl << std::endl << "Available Sensors: " << std::endl;
std::cout << "Dp Dta Dts EV QFE QFF QNH RF" << std::endl  << std::endl;
do {
    std::cout << "Please select Sensor you want to work with: ";
    std::cin >> selectedSensor;

    isSensorValid = std::find(std::begin(availableSensors), std::end(availableSensors), selectedSensor) != std::end(availableSensors);
} while(!isSensorValid);

I also made a method to get Average Daily Value for whole year of data:

double getAverageReadings(int wDay, int wMonth) {
    std::vector<SensorReadings> pData = fm.parsedFile;
    double DewPointTemperature = 0.0;
    for(int r = 0; r < pData.size(); r++) {
        if(pData[r].Month == wMonth) {
            if(pData[r].Day == wDay) {
                if(pData[r].Dp >= 100)
                    DewPointTemperature = DewPointTemperature + cWTKW(pData[r].Dp);
            }
        }
    }
    return DewPointTemperature;
}

This function allows me to get daily average for each day in each month for Dew Point Temperature , what i want to do tho, is to be able do something like this:

double getAverageReadings(int wDay, int wMonth, std::string selectedSensor) {
    /*
        Magic to convert std::string to actual Structure parameter
        Pseudo: 
        param Sensor = convert(selectedSensor, to SensorReadingsParam);
    */
    std::vector<SensorReadings> pData = fm.parsedFile;
    double averageReadingsForSensor = 0.0;
    for(int r = 0; r < pData.size(); r++) {
        if(pData[r].Month == wMonth) {
            if(pData[r].Day == wDay) {
                if(pData[r].Sensor >= 100)
                    averageReadingsForSensor = averageReadingsForSensor + cWTKW(pData[r].Sensor);
            }
        }
    }
    return averageReadingsForSensor;
}

I've never used to work with 'dynamic' parameters before, so im seeking help on this one.

Thank you for your attention, and for any help on this topic!

You have two basic options:

  1. Instead of declaring an explicit variable for each "sensor", use a std::map .

     typedef struct SensorReadings { int Month; int Day; std::map<std::string, double> sensor_value; } SensorReadings;

And store each sensor value in the map, keyed by sensor name, ie sensor_value["Dp"] , sensor_value["Dta"] , and so on.

Then, given a sensor name std::string sensor_name , you can easily looked up the value of that sensor in the map (after checking that it exists, of course).

  1. The second option is to maintain a separate list of sensor names, and a pointer to the corresponding class member, something like:

     static struct { const char *name; double SensorReadings::*value; } sensor_names[]={ { "Dp", &SensorReadings::Dp }, { "Dta", &SensorReadings::Dta }, };

And you can use this table to map sensor names to class members. This is a bit more uglier, but it's doable. I would prefer using the first option, myself.

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