Reputation: 1412
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++).
Upvotes: 0
Views: 1061
Reputation: 3571
As you're accessing list[count + 1]
, you can only iterate from 0 to num - 1.
Upvotes: 3
Reputation: 361272
The actual problem with your code is here:
for(int count = 0; count <= num; count++)
// ^^^ problem!
It should be count < (num-1)
, because you're using list[count + 1]
in the loop-body.
Upvotes: 4
Reputation: 1356
The maximum size of std::string
is defined my its max_size()
function.
Try this:
std::string s;
std::cout << s.max_size();
Upvotes: 1