Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ error in visual studio

I was building a program in c++ in CodeBlocks, and it was running fine. Afterwards I've tried out MS Visual Studio, and it gave me an error message. I'm not quite sure if it is my mistake, and CodeBlocks just doesn't recognise it as well as VS does, or is it a fake error caused by a different 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 appears in last line (bool array). It is says the following: error C2057: expected constant expression; error C2466: cannot allocate an array of constant size 0;

like image 546
Justas Sam Avatar asked Jan 31 '26 01:01

Justas Sam


1 Answers

VLAs (variable length arrays) are not standard C++.

Some compilers allow them as a language extension, but they're frowned upon. I suggest you take the idiomatic approach and use std::vector instead.

like image 123
Luchian Grigore Avatar answered Feb 01 '26 14:02

Luchian Grigore



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!