Can anyone tell me why the following program has an error when it tries to delete the struct pointer at the end of the program?
Thanks
//directives
#include<iostream>
#include<string>
using namespace std;
//structs
struct people
{
string name;
char sex;
int age;
};
//variables
int edit = 0;
char change = ' ';
int size = 0, x = 0;
int main()
{
people person[3];
people *personptr;
cout<<"How many people would you like to enter? ";
cin>>size;
personptr = new people[size];
for(x=0; x<size; x++)
{
cout<<"Enter the 1st name of person #"<<x+1<<": ";
cin>>personptr[x].name;
cout<<endl<<"Enter your sex (m or f): ";
cin>>personptr[x].sex;
cout<<endl<<"Enter your age: ";
cin>>personptr[x].age;
}
// personptr = person;
cout<<"Initial Input"<<endl;
for(x=0; x<size; x++)
{
cout<<"NAME: "<<personptr[x].name<<" SEX: "<<personptr[x].sex
<<" AGE: "<<personptr[x].age<<endl;
}
cout<<"Which person would you like to make a change to? (1,2, or 3)";
cin>>edit;
cout<<"What value would you like to change?
ame,(s)ex,(a)ge ";
cin>>change;
switch(change)
{
case 'n':
case 'N': cout<<"Enter the new name: ";
cin>>personptr[edit-1].name;
break;
case 's':
case 'S': cout<<"Enter the new sex(post sex change): ";
cin>>personptr[edit-1].sex;
break;
case 'a':
case 'A': cout<<"Enter the new age: ";
cin>>personptr[edit-1].age;
break;
default: cout<<"Invalid input";
break;
}
cout<<"Updated Information"<<endl;
for(x=0; x<size; x++)
{
cout<<"NAME: "<<(*personptr).name <<" SEX: "
<<(*personptr).sex<<" AGE: "<<(*personptr).age<<endl;
personptr = personptr + 1;
}
delete [] personptr;
return 0;
}
Thanks
//directives
#include<iostream>
#include<string>
using namespace std;
//structs
struct people
{
string name;
char sex;
int age;
};
//variables
int edit = 0;
char change = ' ';
int size = 0, x = 0;
int main()
{
people person[3];
people *personptr;
cout<<"How many people would you like to enter? ";
cin>>size;
personptr = new people[size];
for(x=0; x<size; x++)
{
cout<<"Enter the 1st name of person #"<<x+1<<": ";
cin>>personptr[x].name;
cout<<endl<<"Enter your sex (m or f): ";
cin>>personptr[x].sex;
cout<<endl<<"Enter your age: ";
cin>>personptr[x].age;
}
// personptr = person;
cout<<"Initial Input"<<endl;
for(x=0; x<size; x++)
{
cout<<"NAME: "<<personptr[x].name<<" SEX: "<<personptr[x].sex
<<" AGE: "<<personptr[x].age<<endl;
}
cout<<"Which person would you like to make a change to? (1,2, or 3)";
cin>>edit;
cout<<"What value would you like to change?

cin>>change;
switch(change)
{
case 'n':
case 'N': cout<<"Enter the new name: ";
cin>>personptr[edit-1].name;
break;
case 's':
case 'S': cout<<"Enter the new sex(post sex change): ";
cin>>personptr[edit-1].sex;
break;
case 'a':
case 'A': cout<<"Enter the new age: ";
cin>>personptr[edit-1].age;
break;
default: cout<<"Invalid input";
break;
}
cout<<"Updated Information"<<endl;
for(x=0; x<size; x++)
{
cout<<"NAME: "<<(*personptr).name <<" SEX: "
<<(*personptr).sex<<" AGE: "<<(*personptr).age<<endl;
personptr = personptr + 1;
}
delete [] personptr;
return 0;
}