Constant in C++!

Hello thank for coming to me!
1
2
3
4
5
6
7
8
9
10
11
//VALID:
const int CONSTANT=100;
int integerArray[CONSTANT]={ 0 };
//but after getting input let's say:
cin>>randomInteger;
int integerArray[randomInteger]; // This is invalid.
// VISUAL STUDIO 13 Says : randomInteger must be a constant; If so?
const int CONSTANT=randomInteger; //This is also invalid. How to get user defined
//Input in a constant variable?
Question: Why? How to resolve this? I know dynamically allocation other than this.
I am using VISUAL STUDIO 13 & codeblocks 12. Mostly VS 13.
You can't. const values are decided at compile time so you can't mess with them during runtime.

The solution is to use dynamic allocation, not to try to work around it.

1
2
cin>>randomInteger;
std::vector<int> integerArray(randomInteger);
Line 8 is perfectly valid, however it is a runtime constant instead of a compile time constant like line 2.
Topic archived. No new replies allowed.