-2

I am trying to pass this test-case (test-stats.cpp) but I am not sure how to read the array of objects using for loop and parse the string in them. I keep getting an error when I try to grab a specific item in the class type weekData[] array. The error says "no suitable user-defined conversion from "WeekData" to "std::__cxx11::string" exists". When I do WeekData line = weekData[i] it works but then I can't put it in the stringstream. How am I supposed to read the strings in the array (main issue) so I can parse through them to get my values stored in a vector?

stats.cpp

double Stats::GetMean(WeekData weekData[], int num){
    vector<int> deathV;
    double mean = 0.0;
    double total = 0.0;

    for(int i = 0; i<num;i++){
        string line = weekData[i]; //ERROR: how do I turn the object to string so I can parse it
        stringstream ss(line);
        while(ss.good()) {
            string week;
            string death;
            getline(ss, week, ',');
            getline(ss, death, ',');
            double deaths = atoi(death.c_str());
            deathV.push_back(deaths);
            total = total+deaths;
        }
    }
   return mean = total/num;
}

double Stats::StDev(WeekData[], int num){
    double sum =0.0;
    double stdev = 0.0;
    vector<int> deathV;
    double mean = 0.0;
    double total = 0.0;

    for(int i = 0; i<num;i++){
        string line = weekData[i]; //ERROR
        stringstream ss(line);
        while(ss.good()) {
            string week;
            string death;
            getline(ss, week, ',');
            getline(ss, death, ',');
            double deaths = atoi(death.c_str());
            deathV.push_back(deaths); //stores deaths
            total = total+deaths;
        }
    }

    for(int i = 0; i<num;++i){
        stdev += pow(deathV[i] - mean, 2);
    }
    return sqrt(stdev / num);
}

test-stats.cpp

WeekData data[] = {
    WeekData("1776-07-04,1"), // Just some static test data.
    WeekData("1776-07-11,2"), // The dates are not important
    WeekData("1776-07-18,3"), //  since we are just trying to test the 
    WeekData("1776-07-25,5"), //  statistical calculations
    WeekData("1776-08-02,6")
};

TEST_CASE("Testing Mean")
{
    // Comparing doubles, can use Approx() to allow some epsilon difference
    CHECK(Approx(3.4) == Stats::GetMean(data, 5));
}

TEST_CASE("Testing Stdev")
{
    CHECK(Approx(1.85).epsilon(0.01) == Stats::StDev(data, 5));
}

TEST_CASE("Testing Degenerate data")
{
    // let's call the mean of no numbers: 0
    CHECK(Approx(0) == Stats::GetMean({}, 0));
    CHECK(Approx(0) == Stats::StDev({}, 0));
}
  • 2
    A `WeekData` is not a string, so you need to either overload `operator<<` to work with a `stringstream` or call whatever function you defined in that class to retrieve the string – UnholySheep Oct 09 '22 at 19:55
  • Seen it somewhere else on the internet. You need to implement `friend ostream& operator<<(ostream& out, const WeekData& week);` It will look something like this: `ostream& operator<<(ostream& out, const WeekData& week) { out << week.date << std::endl; return os;}` – g_1_k Oct 09 '22 at 20:00
  • Reopened. Despite the title, the problem has nothing to do with arrays, so the claimed duplicate isn't appropriate. – Pete Becker Oct 09 '22 at 20:41

1 Answers1

0

The most straightforward approach would be to define a member function which returns a std::string, like std::string MemberData::to_string().

The double Stats::StDev(WeekData[], int num){ function will not compile because you have no definition of the variable weekData which you try to index into later in the function.

oschwartz
  • 1
  • 1