Random string generation function not returning [closed]
Solution 1:
string code[16]
This creates an array of 16 strings. That's probably not what you want. A string is an arbitrarily long sequence of characters. You really only want one string.
I don't know what your alphanumeric()
method does, but if it returns one string, then you're creating 16 strings with 1 character each.
Oh, and returning code[16]
is out of range. code[15]
is the last string. 16 is past the end.
Solution 2:
This return statement
return code[16];
returns a non-existent element of the array
string code[16];
because the valid range of indices for the array is [0, 15].
Instead of declaring an array
string code[16];
what you need is to declare an object of the type std::string like
std::string code;
code.reserve( 16 );
In this case the for loop will look like
for (i = 0; i < 16; i++) {
code += alphanumeric((rand() % 62));
}
and the return statement will look like
return code;