c++ - Array size declarator, can they be calculated? VC++ -


ubernoob question: possible in ms visual c++ declare array size based upon user input?

int usernum; cin >> usernum; const int size = usernum; int myarray[size]; 

it seems if use variable in way initialize constant size, vc++ no longer sees constant purposes of setting array size.

is there way around this?

this can done usign std::vector

#include <iostream> #include <vector>   int main() {   int usernum;   std::cin >> usernum;   std::vector<int> myarray(usernum);   myarray[1]=42;   return 0;    } 

note example has no checks size user has entered.


Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -