Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations danielledunham on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

deleting a struct array pointer 1

Status
Not open for further replies.

random260

Programmer
Joined
Mar 11, 2002
Messages
116
Location
US
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<<&quot;How many people would you like to enter? &quot;;
cin>>size;
personptr = new people[size];

for(x=0; x<size; x++)
{
cout<<&quot;Enter the 1st name of person #&quot;<<x+1<<&quot;: &quot;;
cin>>personptr[x].name;

cout<<endl<<&quot;Enter your sex (m or f): &quot;;
cin>>personptr[x].sex;

cout<<endl<<&quot;Enter your age: &quot;;
cin>>personptr[x].age;
}
// personptr = person;

cout<<&quot;Initial Input&quot;<<endl;
for(x=0; x<size; x++)
{
cout<<&quot;NAME: &quot;<<personptr[x].name<<&quot; SEX: &quot;<<personptr[x].sex
<<&quot; AGE: &quot;<<personptr[x].age<<endl;
}
cout<<&quot;Which person would you like to make a change to? (1,2, or 3)&quot;;
cin>>edit;

cout<<&quot;What value would you like to change? (n)ame,(s)ex,(a)ge &quot;;
cin>>change;

switch(change)
{
case 'n':
case 'N': cout<<&quot;Enter the new name: &quot;;
cin>>personptr[edit-1].name;
break;
case 's':
case 'S': cout<<&quot;Enter the new sex(post sex change): &quot;;
cin>>personptr[edit-1].sex;
break;

case 'a':
case 'A': cout<<&quot;Enter the new age: &quot;;
cin>>personptr[edit-1].age;
break;

default: cout<<&quot;Invalid input&quot;;
break;
}
cout<<&quot;Updated Information&quot;<<endl;
for(x=0; x<size; x++)
{
cout<<&quot;NAME: &quot;<<(*personptr).name <<&quot; SEX: &quot;
<<(*personptr).sex<<&quot; AGE: &quot;<<(*personptr).age<<endl;
personptr = personptr + 1;
}
delete [] personptr;


return 0;
}
 
try making the pointer NULL before deleting it. Also, in this case, you don't need to delete the pointer because the program is ending anyway.
tellis.gif

programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.
 
No, that's not it.

personptr = personptr + 1;


You are changing the pointer.

-pete
 
DOH! Sometimes no matter how many times you read through a program, you just miss what's right in front of your nose lol I have scratched my head on this off and on for a week, never noticed that
personptr = personptr + 1;
hadn't been rem out. Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top