factors is a variable length array, which is a non-standard language feature. You could either create a fixed size array that is as large as you will ever need, or use a container like std::vector:
This should also zero initialise the elements of factors.Code:std::vector<int> factors(halfVal);