Solved
An Access Violation problem.
Posted on 2004-04-01
I'm learning C++ and I have a problem with dynamic arrays.
What is wrong with the following code?
cout<<"Enter numbers:\n";
int count=0;
double avg=0;
double * numbers = new double [count];
cin>>numbers[count];
while (numbers[count]!=-1)
{
avg=avg+numbers[count]; //SOMETIMES THE NUMBERS ARRAY DOESN'T PASS IT'S VALUES INTO THE LOOP
double * temp = new double [count];
for (int i=0;i<count;i++)
temp[i]=numbers[i];
count++;
numbers=NULL;
delete [] numbers;
double * numbers = new double [count];
for (i=0;i<count;i++)
numbers[i]=temp[i];
temp=NULL;
delete [] temp;
cin>>numbers[count]; //THIS LINE PRODUCES AN ACCESS VIOLATION.
};
cout<<"\nThe avarage: "<<avg;
cin.get();
return 0;
By the way, I use Visual C++ 6.0.