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));
}