The issue with your code is that you are declaring what is called a variable length array which is not part of C++ (though it is valid C code). See this for an explanation as to why.
You can achieve what you are trying to do in a few different ways though:
You could dynamically allocate an array using a user provided size:
#include <iostream>
#include <memory>
int main(int argc, char** argv)
{
std::size_t a =0;
std::cout<<"Enter size of array";
std::cin>>a;
std::unique_ptr<int[]> arr(new int[a]);
//do something with arr
//the unique ptr will delete the memory when it goes out of scope
}
This approach will work but may not always be ideal, especially in situations where the size of the array may need to change frequently. In that case I would recommend using a std::vector
:
#include <iostream>
#include <vector>
int main(int argc, char** argv)
{
std::size_t a =0;
std::cout<<"Enter size of array";
std::cin>>a;
std::vector<int> arr(a);//arr will have a starting size of a
//use arr for something
//all of the memory is managed internally by the vector
}
You can find the reference page here.