I was wondering if I do this:
int TOPE , vector[TOPE] ;
cin >> TOPE ;
Does vector get the amount of elements I input or does it have a number that I can't control?
I was wondering if I do this:
int TOPE , vector[TOPE] ;
cin >> TOPE ;
Does vector get the amount of elements I input or does it have a number that I can't control?
The variable vector
is a Variable-Length Array (VLA). Standard C++ doesn't support that, but some compiler supports that as extension.
The variable TOPE
is not initialized at the point where the variable vector
is declared, so its size is indeterminate.
To set the size of VLA to the number entered, you should write like this:
int TOPE;
cin >> TOPE ;
int vector[TOPE] ;
To do it in more standard way, you should use std::vector
instead of VLA:
int TOPE;
cin >> TOPE ;
std::vector<int> vector(TOPE);
Firstly, VLA
s, or variable-length array is not a C++ standard, although some compiler like gcc
does support it. More info : Why aren't variable-length arrays part of the C++ standard?
Secondly, compilers read in a top-down fashion. Changing a variable after it has been used as length for an array isn't going to change the array in any way.
To declare it properly, declare it like this :
int TOPE; std::cin >> TOPE;
int vector[TOPE];