QUOTE(BrainStew @ 21 Aug, 2007 - 09:06 PM)

I think I'm going crazy... I'm trying to initialize an array to a random size between 1 and 20. I must be too tired or something.
CODE
#include <iostream>
#include <ctime>
int main(void)
{
srand((unsigned)time(NULL));
int iRand_Num = rand()%20+1;
int iArray[iRand_Num] = {0};
system("pause");
}
Thanks... I'm going to go jump off my roof now.
The size of a statically allocated array must be constant i'm afraid.
You have two options - the first (preferable) is to forget arrays, because 'C' style arrays are a bit of a nuisance, and go with a vector from the C++ STL (standard template library). A vector is basically a C++ array, although it automatically resizes itself to suit the number of elements you wish to add to it, and has accessor methods with bounds checking.
CODE
#include <iostream>
#include <vector>
#include <ctime>
#include <cmath>
int main()
{
srand( time(0) );
int size = rand() % 20 + 1;
std::vector<int> my_vec(size);
std::cout << my_vec.size();
}
The other way (less preferable, but if you insist on having a raw C-style array..) is to create it dynamically with
newCODE
int rand_num = rand() %20 + 1;
int* arr = new int[rand_num];
Doing this, you
must remember to
delete [] the array when you're done with it, else your program will have a memory leak. (The great thing about vectors is that they clean up after themselves and don't "leak").