Why I can't use this code when I want to assign the size of array entered by user to array?
int n;
cin>>n;
int array[n];
And is there another way of doing this instead of using this construction?
int n;
cin>>n;
int *array;
array = new int[n];
Why I can't use this code when I want to assign the size of array entered by user to array?
int n;
cin>>n;
int array[n];
And is there another way of doing this instead of using this construction?
int n;
cin>>n;
int *array;
array = new int[n];
According to O'Reilly "C++ In A Nutshell" (2003),
An array is specified with a constant size in square brackets
Since your variable n is not a constant, it can't be used to specify the size of the array.
The same paragraph also says,
For an array-like container whose size can change at runtime, see <vector> in Chapter 13.
Sorry, but you are not allowed to have this construction.