[Solved] C++ : Why it dont work?


as the other answer, initialize k, move if outside of loop

vector<int> arr(n);
int k = 0;
for(int arr_i = 0;arr_i < n;arr_i++){
   cin >> arr[arr_i];
    k = k + arr[arr_i];
    //cout << "arr = " << arr[arr_i] << " k " << k << endl;     //  [0]    
}
cout << k;

as your question, there is no need std::vector anymore

int sum = 0, num;
for(int arr_i = 0;arr_i < n;arr_i++){
   cin >> num;
   sum = sum + num;
}
std::cout << sum << std::endl;

1

solved C++ : Why it dont work?