I tried to write for loop outside the function. But it is throwing an error. Why it’s showing like that?

  c++, c++11

Here is the code as you can see, I am not able to run it, its throwing error.

#include <iostream>
#include <thread>
using namespace std;
int sum = 0;
int arr[500];
for (int i = 0; i < 500; i++) {
    arr[i] = rand() % 100;
    cout << arr[i] << endl;
    void arraySum() { sum = sum + arr[i]; }
}
main() {
    std::thread t1(arraySum);
    t1.join();
    for (int i = 0; i < 10; i++) {
        cout << "Sum of : " << sum;
    }
}

Source: Windows Questions C++

LEAVE A COMMENT