1
int n;
cout<<"Enter the value of n"<<endl;
cin>>n;
int arr[n];

why do I get these error expression must have a constant value

VANSH DOSI
  • 11
  • 1
  • 3
    The value `n` is not constant. Variable-length arrays are not standard C++, hence the error. Instead, use `std::vector` for a dynamically sized container with contiguous memory allocation. – paddy May 16 '22 at 10:27
  • Specifically here: `#include` at the beginning and then `std::vector arr(n);` instead of `int arr[n];`. In most situations it is better to use `std:vector` (or maybe `std::array`) than a raw C-style array. – user17732522 May 16 '22 at 10:30

0 Answers0