Initialize int array with length of a input string in c++ -
this question has answer here:
i receive these errors 1. cannot allocate array of constant size 0 2. expected constant expression 3. 'numbers' : unknown size
#include <iostream> #include <string> using namespace std; int main() { string str; int input_num; int sum; cout << "enter number:" << endl; getline(cin, str); const int length = str.length(); cout << "length:" << length<<endl; //input_num = stoi(str); int numbers[length]; return 0; }
replace use of array std::vector
, , initialize elements 0
.
std::vector<int> numbers(length, 0);