大侠帮忙看看有什么错

来源:百度知道 编辑:UC知道 时间:2024/06/11 16:54:11
#include"iostream.h"
#include"stdio.h"
#include"stdlib.h"
#include"header.h"
#define LEN sizeof(struct employee )
struct employee
{int num;
char name[30];
int age;
char sex;
char add[30];
struct employee *next;
};
int n;////节点计数器
struct employee *input()
{
struct employee *head;
struct employee *p1,*p2;
n=0;
p1=p2=(struct employee *)malloc(LEN);
cout<<"请输入职工信息,输入0 结束输入"<<endl;
cout<<"请输入职工号";
cin>>p1->num;
cout<<endl<<"请输入职工姓名";
cin>>p1->name;
cout<<endl<<"请输入职工年龄";
cin>>p1->age;
cout<<endl<<"请输入职工性别";
cin>>p1->sex;
cout<<endl<<"请输入职工籍贯";
cin>>p1->add;
head=NULL;
while(p1->num!=0)
{
n++;

p=p->next;
上面的循环中没有存储为0职工号的员工,但是这一语句追后跳出时,调用了内存不该调用的位置
do
{
printf("%d %s %d %c %s\n",p->num,p->name,p->age,p->sex,p->add);
p=p->next;
}
while(p->next!=NULL);
应该就可以了。
还有再记录下一条前给一个判断比输入0更好一点