-3

I have the following function which aims to return a 16 character long alphanumerical string composed of 62 characters char type alphanumeric() function can return (A-Z, a-z alongside numbers 0-9).

string random() {
    string code;
    int i;
    srand(time(NULL));

    for (i = 0; i < 16; i++) {
        code[i] = alphanumeric((rand() % 62));
    }

    return code;
}

At the return line the code breaks.

  • Does this answer your question? [Return array in a function](https://stackoverflow.com/questions/3473438/return-array-in-a-function) – Ghasem Ramezani Aug 24 '21 at 18:18
  • 1
    I presume that you have either disabled compiler warnings or you are not looking at them. Your compiler _wants_ to talk to you about `return code[16];` – Drew Dormann Aug 24 '21 at 18:26
  • 1
    So does [your rubber duck](https://en.wikipedia.org/wiki/Rubber_duck_debugging). – user4581301 Aug 24 '21 at 18:26

3 Answers3

2
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.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Joseph Larson
  • 8,530
  • 1
  • 19
  • 36
1

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;
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • I would use `resize()` (or the string constructor) instead of `reserve()`, and then continue using `code[i] = ...` instead of `code += ...`. Otherwise, you can still use an array for `code`, just change it into a `char[]` instead, and then let the `return` implicitly convert the `char[]` into a `string` for you, eg: `char code[16]; srand(time(NULL)); for (int i = 0; i < 16; i++) { code[i] = alphanumeric(rand() % 62); } return code;` – Remy Lebeau Aug 24 '21 at 18:52
0

In your code provided, you're trying to return the last element of the "code" array which would be "code[15]" because array's start at 0 and you're going out of the bounds of the array.

To fix this function's issue just put: "return code;"

[Edit]

Taking into consideration that the "alphanumeric" function takes in a single integer parameter (assuming in this case index) and returns [0-9A-Za-z] depending on the index, a revised version of this function which uses the "#include <string>" header file with minimal modifications to the code works as intended:

#include <iostream>
#include <string>

using namespace std;

char alphanumeric(int index)
{
    static const char alphanum[] =
        "0123456789"
        "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        "abcdefghijklmnopqrstuvwxyz";
    return alphanum[index];
}

//We could also specify a length parameter to return different randomized string sizes
string random()
{
    string code;
    int i;
    srand(time(NULL));

    for (i = 0; i < 16; i++)
    {
        code += alphanumeric((rand() % 62));
    }

    return code;
}

int main() {
    cout << random() << endl;
}
Mal1t1a
  • 74
  • 4
  • Except that `return code;` will not work when `code` is a `string[]` and the function is declared to return a single `string`. You would have to change `code` into a `char[]` instead. – Remy Lebeau Aug 24 '21 at 18:55