Search code examples
c++arraysstring-length

get the length of a string and assign it to an array


Total c++ newbie here.

I have a problem, where I can not assign the number of character in a string to an array size, like so..😓

string outStr;
ifstream input("read.txt");
getline(input, outStr);

int const n = outStr.length();
int arr[n];

error msg --> expression must have a constant value. although i have declared the "const"

Thanks in advance✌.


Solution

  • C++ does not support Variable Length Arrays. Use a std::vector instead.