Home > Software engineering >  How to make an object and put it into an array using a loop, so if I want to add data all I have to
How to make an object and put it into an array using a loop, so if I want to add data all I have to

Time:12-04

How to make an object and put it into an array? I want to make an array of DailyStats with the parameterized constructor. I want to use a loop to add to the array of objects.

#ifndef DAILYSTATS_H
#define DAILYSTATS_H

#include <iostream>
#include <vector>
using namespace std;

class DailyStats
{
    public:
        DailyStats();
        DailyStats(string content);
        void parse(const string& line);
        double mean();
        string getDate();
        void setDate(string newDate); 

    private:
        string date;
        double temperatureValues[];
};

#endif
#include <iostream>
#include "DailyStats.hpp"
#include <vector>
#include <cmath>
using namespace std;
    
DailyStats::DailyStats()
{
    date = "";
    count = 0;
    temperatureValues[0];
}

DailyStats::DailyStats(string content)
{
    parse(content);
    temperatureValues[24];
    count = 0;
}

void DailyStats::parse(const string& line)
{
    string random = "";
    string random1 = "";
    char del = ' ';
    int count2 = 0;

    for(int i = 0; i <= (int)line.size(); i  )
    {
        if(count < 10)
        {
            random1  = line[i];
            setDate(random1);
            count  ;
        }

        if(line[i] != del && count == 10)
        {
            random  = line[i];
        }
        else if (line[i] == del && count == 10)
        {
            temperatureValues[count2] = stod(random);
            random = "";
            count2  ;
        }
    }
}

double DailyStats::mean()
{
    count = 1;
    double num = 0;

    while(count <= 24)
    {
        num  = temperatureValues[count];

        if(count == 24)
        {
            num = num/24;
        }

        count  ;
    }

    return ceil(num * 100.0) / 100.0;
}

string DailyStats::getDate()
{
    return date;
}

void DailyStats::setDate(string newDate)
{
    date = newDate;
}
#include <iostream>
#include <string>
#include "DailyStats.hpp"
using namespace std;

int main()
{
    string str0 = "06/01/2021 74.85 71.58 78.68 71.55 78.14 72.36 76.89 71.35 79.94 78.87 78.07 75.78 77.86 74.04 76.56 72.96 75.07 74.02 70.21 75.56 79.61 72.97 75.29 73.33 ";
    string str1 = "06/01/2021 74.85 71.58 78.68 71.55 78.14 72.36 76.89 71.35 79.94 78.87 78.07 75.78 77.86 74.04 76.56 72.96 75.07 74.02 70.21 75.56 79.61 72.97 75.29 73.33 ";
    string str2 = "06/01/2021 74.85 71.58 78.68 71.55 78.14 72.36 76.89 71.35 79.94 78.87 78.07 75.78 77.86 74.04 76.56 72.96 75.07 74.02 70.21 75.56 79.61 72.97 75.29 73.33 ";
    string str3 = "06/01/2021 74.85 71.58 78.68 71.55 78.14 72.36 76.89 71.35 79.94 78.87 78.07 75.78 77.86 74.04 76.56 72.96 75.07 74.02 70.21 75.56 79.61 72.97 75.29 73.33 ";

    DailyStats info[4];

    info[1] = { {str0} };

    cout << info[1].mean() << endl;


    return 0;
}

As you can see with my code, I have different strings that I would like to put in my objects to be used as the variable for my parameterized constructor. So, when I have that done, I can then call my mean() function so it can get the different means of the different data.

I tried it without the array, and it all works fine, but I can't figure out how to get the array to work.

CodePudding user response:

The main problem I see is that your temperatureValues[] array is unbound at compile-time. That will not work. Since you don't know the size of the array until runtime, use std::vector instead (in fact, you already have #include <vector> in your code), eg:

#ifndef DAILYSTATS_H
#define DAILYSTATS_H

#include <vector>
#include <string>

class DailyStats
{
    public:
        DailyStats() = default;
        DailyStats(const std::string& content);
        void parse(const std::string& line);
        double mean() const;
        std::string getDate() const;
        void setDate(const std::string& newDate); 

    private:
        std::string date;
        std::vector<double> temperatureValues;
};

#endif
#include "DailyStats.hpp"
#include <cmath>
#include <sstream>
    
DailyStats::DailyStats(const string& content)
{
    parse(content);
}

void DailyStats::parse(const string& line)
{
    std::istringstream iss(line);
    double value;

    iss >> date;

    temperatureValues.clear();
    while (iss >> value)
        temperatureValues.push_back(value);
}

double DailyStats::mean() const
{
    double num = 0.0;

    if (!temperatureValues.empty())
    {
        for(size_t count = 0; count < temperatureValues.size();   count)
        {
            num  = temperatureValues[count];
        }
        num = std::ceil((num / temperatureValues.size()) * 100.0) / 100.0;
    }

    return num;
}

std::string DailyStats::getDate() const
{
    return date;
}

void DailyStats::setDate(const std::string& newDate)
{
    date = newDate;
}
#include <iostream>
#include <string>
#include "DailyStats.hpp"
using namespace std;

int main()
{
    string str0 = "06/01/2021 74.85 71.58 78.68 71.55 78.14 72.36 76.89 71.35 79.94 78.87 78.07 75.78 77.86 74.04 76.56 72.96 75.07 74.02 70.21 75.56 79.61 72.97 75.29 73.33 ";
    string str1 = "06/01/2021 74.85 71.58 78.68 71.55 78.14 72.36 76.89 71.35 79.94 78.87 78.07 75.78 77.86 74.04 76.56 72.96 75.07 74.02 70.21 75.56 79.61 72.97 75.29 73.33 ";
    string str2 = "06/01/2021 74.85 71.58 78.68 71.55 78.14 72.36 76.89 71.35 79.94 78.87 78.07 75.78 77.86 74.04 76.56 72.96 75.07 74.02 70.21 75.56 79.61 72.97 75.29 73.33 ";
    string str3 = "06/01/2021 74.85 71.58 78.68 71.55 78.14 72.36 76.89 71.35 79.94 78.87 78.07 75.78 77.86 74.04 76.56 72.96 75.07 74.02 70.21 75.56 79.61 72.97 75.29 73.33 ";

    DailyStats info[4];

    info[0].parse(str0);
    info[1].parse(str1);
    info[2].parse(str2);
    info[3].parse(str3);

    cout << info[0].mean() << endl;
    cout << info[1].mean() << endl;
    cout << info[2].mean() << endl;
    cout << info[3].mean() << endl;

    return 0;
}
  • Related