The program below is executing fine but when I enter the last of 10
numbers and the final output(shows the average of the 10 numbers) is displayed,
I get the following error message:
Run-Time Check Failure #2 - Stack around the variable 'numbers' was
corrupted.
If someone could explain what this error message is for, I would appreciate it.
Thanks
FWD
Code:
#include "stdafx.h"
#include <iostream>
using namespace std ;
double calculateAvg(double sumNum, int numCount); // calculateAvg's Prototype
int main()
{
/****************************************/
/*Explanation of what the program does. */
/****************************************/
cout << "This program will prompt you 10 times to enter a number. " << endl;
cout << "After you have entered the last number the program will " << endl;
cout << "divide the sum of the numbers you entered by 10. " << endl;
cout << "The program will then display the result of that calculation " << endl;
cout << "as the average of those 10 numbers." << endl << endl;
/*********************************************************/
/* Declaration and Initialization of Variables and Array */
/*********************************************************/
int const nSize = 9; // integer constant to hold the size of array numbers[]
double numbers [nSize]; // Declaration of the array numbers[] to hold 10 values entered by user.
int i; // Variable i declared to act as counter in the for l
double sum; // Variable sum declared to hold a running sum of numbers entered.
sum = 0; // Initializing sum to a value of 0.
/****************************************************************/
/* The following for-loop is used to prompt the user for input,
store the users input into the Array numbers[nSize],
and keep a running sum of the numbers entered into the array.*/
/****************************************************************/
for (i=1; i<=nSize+1; i++)
{
/*****************************************************************/
/* Selection statement to determine the prompt given to the user.*/
/*****************************************************************/
if (i == 1)
{
cout << i << ". Enter the first positive-number and press Enter: ";
cin >> numbers[i - 1];
sum = sum + numbers[i - 1];
cout << endl;
}
else if (i == 10)
{
cout << i << ". Enter the last positive-number and press Enter: ";
cin >> numbers[i - 1];
sum = sum + numbers[i - 1];
cout << endl << endl;
}
else
{
cout << i << ". Enter the next positive-number then press Enter: ";
cin >> numbers[i - 1];
sum = sum + numbers[i - 1];
cout << endl;
}
}
/***************************************************************************************/
/* Output statement which calls the function calculateAvg(double sumNum, int numCount)
with the parameters sum and (nSize + 1). The output statement outputs the value
returned by the function, which is the avg of the 10 numbers the user entered. */
/***************************************************************************************/
cout <<"The average of all ten numbers that you entered is: ";
cout << calculateAvg(sum, (nSize + 1)) << endl << endl;
return 0;
}
/***********************************************************/
/* The following function computes the the Average of all 10
numbers entered by the user.
Inputs:
sumNum (double), numCount (integer)
Output:
avgNum (double) */
/***********************************************************/
double calculateAvg(double sumNum, int numCount)
{
double avgNum;
avgNum = sumNum / numCount;
return(avgNum);
}