My code bellow is supposed to take take the average of all numbers entered in a user defined array. Everything is working fine except I need to delete nums at the end of the program. When I run the program it crashes/aborts with "heap corruption detected." If I comment out the delete statement it works and this does not happen but I need to have it in the program. Can someone explain why this is happening?
#include <iostream>
using namespace std;
int main()
{
cout << "Hi, Let me find the Average! \n \n";
int SIZE = 0;
double *nums = new double[SIZE];
int i = 0;
double total = 0.0;
double num = 0.0;
double average = 0.0;
cout << "Please enter how many numbers to be entered: ";
cin >> SIZE;
do{
cout << "Please enter the " <<i+1<< "st/th number : ";
cin >> num;
nums[i] = num;
num = 0;
i++;
if (i == SIZE) break;
} while (i < SIZE);
cout << "Here are the numbers you entered: \n";
for (int n = 0; n < SIZE; n++) {
cout << nums[n] << "\n";
}
for (int n = 0; n < SIZE; n++) {
total += nums[n];
}
cout << "You chose to enter " << SIZE << " numbers. \n";
cout << "The total of all the numbers you entered is : " << total << "\n";
average = total / double(SIZE);
cout << "The average of all the numbers that you added is: " << average << "\n";
//delete[] nums; // Here is the issue
}