|
我写了个简单的求平均数的程序,调试的时候弹出一个错误提示,选择忽略可以继续运行
...个人估计是32行 delete [] ptemp;有问题,内存上的错,但是不知但改怎样,修改.
请大家帮忙,谢谢
#include <iostream>
#include <iomanip>
using std::cout;
using std::cin;
using std::endl;
using std::setw;
int main()
{
int arraySize = 5;
int count = 0;
double value = 0.0;
double* pvalueBox = new double[arraySize];
double* ptemp;
for(;;)
{
cout << endl << "Please input a value or enter 0 to end." << endl;
cin >> value;
if (value == 0)
break;
if (count > arraySize)
{
arraySize += 5;
ptemp = pvalueBox;
pvalueBox = new double[arraySize];
for (int i = 0; i < count; i++)
pvalueBox[i] = ptemp[i];
delete [] ptemp;
}
pvalueBox[count] = value;
count++;
}
double sum = 0;
for(int i = 0; i < count; i++)
{
sum += pvalueBox[i];
cout << setw(9) << pvalueBox[i];
if ((i+1)%5 == 0)
cout << endl;
}
cout << endl << "The average is " << setw(10) << (sum/(count)) << endl;
return 0; |
|