0

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];
k1ber
  • 180
  • 1
  • 13

1 Answers1

-1

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.

Randy Zack
  • 148
  • 6