problem with a function.

I'm trying to make function that returns a pointer to a random string. But i get a compiler error on line 13.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
const string chartabel[] = {
	"qwertyuiopasdfghjklzxcvbnm",
	"QWERTYUIOPASDFGHJKLZXCVBNM",
	"01234567890123456789012345",
	"!@#£¤$%&*^~!@#£¤$%&*^~!@#£",
};

string * randomgen(){
	int * length = new int(rand() % 5 + 12);
	string * password = new string[*length];
	*password = "password";
	for(int x = 0; x > *length; x = x + 1){
		*password[x] = chartabel[rand()%4][rand()%26];
	}

	return password;
}
try to remove the dereference ( * ) operator since you are already dereferencing by the use of the ( [ ] ) operator
Last edited on
password is an array of strings.
the first element of the array is initialised to "password", by using the name of the whole array as a pointer to it's first element
In your loop you try and overwrite it immediately, because password[0] is the same as *password, and you start with x = 0;
Your loop never gets to run, because your condition x > *length fails immediately, since you initialised x to 0 in the same line. Unless the length of your string is somehow negative.
On line 13, youre trying to initialise the other elements, but you're using a char instead of a string. So through the magic of default construction, you would end up with an array of one character long strings. chartabel[x][y] is a single character, not a string.
Topic archived. No new replies allowed.