Very simple array problem

I'm making a function that creates a pin (all one string) that is created from random ints. My problem is that my array initialization isn't working for some reason

1
2
3
4
5
6
7
8
9
10
11
12
13
string randomPassword(int size)
{
	string pass;
	int passArr[size];

	for (int i = 0; i < size; i++)
	{
		passArr[i] = rand() % 10;
		static_cast<string>(passArr[i]);
		pass += passArr[i];
	}
	return pass;
}
Last edited on
Maybe this would work?
 
pass += to_string( passArr[i] );

http://www.cplusplus.com/reference/string/to_string/
My problem at the moment is line 4.
size is underlined and it says Error: expression must have a constant value.
Oh. O.O

You cannot declare memory in such a away. The compiler must know the size of the memory block you're asking for when you use such a bracket notation, so using the parameter passed in not syntatically correct.

You are free to use pointers in conjunction with the new operator however.
 
int *passArr = new int[size];


This does functionally the same thing you are trying to do on line 4, and you are able to access the elements the same way as you would an array (i.e. passArr[0], passArr[1],...)
Ok that is working. Now my problem is converting each individiual number in the array into a string and adding them all up so the pin is just one string.
Maybe its just easier to create int variable that is like 10 digits long and use the rand function. Wow what am i doing. Thanks for trying to help though.
Topic archived. No new replies allowed.