0

Anyone know why Visual Studio 2019 is the only compiler to giving me error - Expression must have a constant value? Line 11(E0028). In other compilers everything is fine, the problem is only with VS

#include <iostream>
#include <math.h>

using namespace std;

const double pi = acos(-1);

signed main() {
    int n;
    cin >> n;
    double mass[n];
    for (int i = 0; i < n; i++) cin >> mass[i];
    int c = 0;
    for (int i = 1; i <= n; i++) {
        mass[i - 1] /= i;
        if (mass[i] < 0) c++;
    }
    cout << c << endl;
}
  • Because that's what happens when you use non-standard C++, like variable length arrays. Some compilers support them, but not every compiler does. If you intend to write compliant C++ code, you should not use variable length arrays, but rather `std::vector`s. – Sam Varshavchik Nov 28 '20 at 02:34
  • 2
    Yet another victim of the `g++` compiler settings. -- *In other compilers everything is fine* -- There is *no* C++ book that has any of that syntax or code (`double mass[n];`) that is causing the issue. You are obviously learning C++ through very poor websites. – PaulMcKenzie Nov 28 '20 at 02:34
  • _“the only compiler”_ — Not if you enable all compiler warnings. – Sebastian Simon Nov 28 '20 at 02:38
  • If VS is the only compiler that issues diagnostics for the shown code, then the problem is not with VS - it is actually all of the other compilers you've used that are incorrect, since variable length arrays are not part of standard C++. That said, most other compilers are configured by *default* to being incorrect in this case, and it is necessary to enable options that cause them to be more strict (and correct) in their diagnosis of problems. – Peter Nov 28 '20 at 05:25

0 Answers0