Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

false expression must have a constant value in visual studio 2022 c++

hello there i was writing a code in a compiler but my compiler had this error :"false expression must have a constant value" in one of the program lines
i used other compilers but they didn’t say this and i could write my program , but in visual studio 2022 it gives me the error
the sample of the program is :

 stack<char> stack;
 queue<char> queue;
string str;
cin >> str;
char ch[str.length()];

the error is in the

char ch[str.length()];

i dont know how to fix this
i would be glad if you guys help me in this

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Variable-length arrays is not C++ standard, see here. Because str.length() is known at runtime, but the size of the array has to be known at compile-time, this will cause an error.

You should use std::vector instead:

Replace:

char ch[str.length()];

With:

std::vector<char> ch(str.length());
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading