C ++ error in visual studio

I created a program in C ++ at CodeBlocks and it worked fine. Afterwards I tried MS Visual Studio and it gave me an error message. I'm not really sure if this is my fault and CodeBlocks just doesn't recognize it like VS, or it is a fake error caused by another compiler.

Here's the code:

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main(){

    string widthin;
    int width, height;

    cout << "Enter the size of the puzzle (WIDTH HEIGHT) : " << endl;
    getline(cin, widthin);
    stringstream(widthin) >> width >> height;
    if (width == 0 || height == 0 || width >= 10000 || height >= 10000){
        cout << "Value cannot be a zero nor can it be a letter or a number higher than 10000!" << endl;

        return main();

    cout << width << " x " << height << endl << endl;

    const int plotas = width*height;
    int p;

    bool board[2][plotas];


The error is displayed on the last line (bool array). It says the following: error C2057: constant expression expected; error C2466: cannot allocate array of constant size 0;


source to share

1 answer

VLAs (Variable Length Arrays) are not standard C ++.

Some compilers allow them to expand the language, but they are frowned upon. I suggest you take an idiomatic approach and use instead std::vector




All Articles