I think the error has to do with my string being too large. If the function worked correctly, I doubt that I would be anywhere near the max string size (unless that really is the problem? I doubt it because this is for a HW assignment and we need to return output with the song lyrics). What is throwing this error? How big can the string get?
Error:
terminate called after throwing an instance of 'std::length_error'
what(): basic_string::_S_create
Aborted (core dumped)
Function:
string generateSong(string list[], int num)
{
string output;
for(int count = 0; count <= num; count++)
output += list[count] + " bone connected to the "
+ list[count + 1] + " bone\n";
return output;
}
Contents of list[]:
string list[9] =
{
"toe",
"foot",
"leg",
"knee",
"hip",
"back",
"neck",
"jaw",
"head"
};
num is 9. Is output really becoming too big? Everything compiles fine (using g++).
The actual problem with your code is here:
It should be
count < (num-1), because you’re usinglist[count + 1]in the loop-body.