Reputation: 1
Here is what I am basically trying to do
cout<<"Enter size of array"<<endl;
cin>>size;
int my_array[size];
The compiler complains that size must be constant.
Upvotes: 0
Views: 67
Reputation: 43
I think you can use the new keyword here to allocate memory dynamically just modify your code like this and it should be fine
int *my_array;
cout<<"Enter size of array"<<endl;
cin>>size;
my_array=new int[size];
Upvotes: 1
Reputation: 254431
Indeed, arrays must have constant size. You'll want a dynamic array:
std::vector<int>(size);
Upvotes: 2