I am trying to return a string pointer from my method GetOutliers() to pass my test case but it is not working and I keep getting the error "FAILED: {Unknown expression after the reported line} due to unexpected exception with message: std::bad_alloc . I have already tried to define the method without the pointer but that doesn't work because in the test case it does string* outlier = bama.GetOutliers() so it is expecting the output to be a string pointer. I have also tried it with the pointer and I made it return an array with my string stored in it. Both attempts gave me the same error.
I need help trying to figure out how to return the string totalString and hopefully pass my test case.
Attempt 1:
string* State::GetOutliers(){
vector<string>outliers;
double mean = GetMeanDeaths();
double std = Stdev();
int deathNum = week->GetDeathCount();
string getOut = " - total deaths: ";
string death = to_string(deathNum);
string week = week;
string* totalString;
string sentence;
int outCount;
for(int i = 0; i<numberOfDataPoints;i++){
if(deathNum >= (2*std+mean)){
sentence = week+getOut+death;
}
outCount++;
}
totalString = &sentence;
return totalString;
}
Attempt 2:
string* State::GetOutliers(){
vector<string>outliers;
double mean = GetMeanDeaths();
double std = Stdev();
int deathNum = week->GetDeathCount();
string getOut = " - total deaths: ";
string death = to_string(deathNum);
string week = week;
string totalString;
string stringArray[numberOfDataPoints];
string sentence;
int outCount;
for(int i = 0; i<numberOfDataPoints;i++){
if(deathNum >= (2*std+mean)){
totalString = week+getOut+death;
stringArray[i] = totalString;
}
outCount++;
}
return stringArray;
}
Test case:
TEST_CASE("Testing State object")
{
State bama("Alabama");
CHECK("Alabama" == bama.GetName());
bama.AddWeek("2020-05-02,2000");
bama.AddWeek("2020-05-09,2000");
bama.AddWeek("2020-05-16,100");
bama.AddWeek("2020-05-23,2000");
bama.AddWeek("2020-05-30,2000");
bama.AddWeek("2020-06-06,2000");
CHECK(Approx(1683.33).epsilon(0.01) == bama.GetMeanDeaths());
CHECK(Approx(708.08).epsilon(0.01) == bama.Stdev());
CHECK(1 == bama.GetOutlierCount());
std::string* outliers = bama.GetOutliers();
CHECK("2020-05-16 - total deaths: 100" == outliers[0]); //TEST CASE FOR GetOutliers()
delete [] outliers;
}