2021-12-04

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 do is make a new string

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.



from Recent Questions - Stack Overflow https://ift.tt/3DgHoTb
https://ift.tt/eA8V8J

No comments:

Post a Comment