*** Declaring Array size from User Input ***
-
Hi Everyone, I would like to use an array that has a size that is determined by the user input. But I'm getting this problem of " array must contain constant " from this array declaration: int UserInput; int TheArray[UserInput]; How do I declare the size of the array with user input?? if anyone knows, Please let me know. Thanks in Advance!
-
Hi Everyone, I would like to use an array that has a size that is determined by the user input. But I'm getting this problem of " array must contain constant " from this array declaration: int UserInput; int TheArray[UserInput]; How do I declare the size of the array with user input?? if anyone knows, Please let me know. Thanks in Advance!
1. If it is a maximum possible array size, you can declare array of this size, like this: #define MAX_USER_INPUT 255 int TheArray[MAX_USER_INPUT]; Do not forget to check UserInput in this case. if (UserInput > MAX_USER_INPUT) // show error message 2. Or use new() and delete() int *TheArray = NULL; BOOL InitArray(int *&TheArray, int UserInput) { if (UserInput <= 0) return FALSE; if (TheArray) delete[] TheArray; TheArray = new int[UserInput]; return TheArray != NULL; } Don't forget to delete[] TheArray.
-
Hi Everyone, I would like to use an array that has a size that is determined by the user input. But I'm getting this problem of " array must contain constant " from this array declaration: int UserInput; int TheArray[UserInput]; How do I declare the size of the array with user input?? if anyone knows, Please let me know. Thanks in Advance!
int theUserInput; theUserInput = 10; // or from user input int *array; array = new int[theUserInput]; array[0] = 5; // whatever