程序运行时出现内存错误,怎么回事
#include "iostream.h"#include "malloc.h"
#include "conio.h"
#include "stdlib.h"
#include "string.h"
#define LEN sizeof(struct student)
struct student
{
int num;
int math;
int chinese;
int english;
char name[20];
float aver;
float sum;
struct student *next;
};
void type();
void insert(struct student *stu);
void look(struct student *stu);
void lookall();
void add();
void query_by_name();
void query_by_num();
void del();
struct student *head=NULL;
int total=0;
void main()
{
system("color 70");
int choice;
int flag=1;
do
{
cout<<"\n\t\t\t欢迎来到学生管理系统\n"<<endl;
cout<<"\n\t\t\t--------------------\n"<<endl;
cout<<"\t\t\t1:键入学生的信息\n"<<endl;
cout<<"\t\t\t2:查看录入学生的信息\n"<<endl;
cout<<"\t\t\t3:按姓名查看\n"<<endl;
cout<<"\t\t\t4:删除学生的信息\n"<<endl;
cout<<"\t\t\t5:增加一个学生\n"<<endl;
cout<<"\t\t\t6:按学号查看\n"<<endl;
cout<<"\t\t\t0:退出\n"<<endl;
cout<<"\t\t请输入你的选择:"<<endl;
cin>>choice;
switch(choice)
{
case 1: system("CLS");type();break;
case 2: system("CLS");lookall();break;
case 3:system("CLS");query_by_name();break;
case 6: system("CLS");query_by_num();break;
case 4: system("CLS");del();break;
case 5: system("CLS");add();break;
case 0:flag=0;break;
default :cout<<"错误!"<<endl;break;
}
}while(flag!=0);
}
void type()
{
struct student *p,*q;
q=head;
char nam[20];
p=(struct student *)malloc(LEN);
cout<<"在姓名处输入!结束"<<endl;
cout<<"姓名:"<<endl;
cin>>nam;
while(strcmp(nam,"!")!=0)
{ strcpy(p->name,nam);
cout<<"学号:"<<endl;
cin>>p->num;
cout<<"数学成绩:"<<endl;
cin>>p->math;
cout<<"外语成绩:"<<endl;
cin>>p->english;
cout<<"语文成绩:"<<endl;
cin>>p->chinese;
p->sum=(float)p->chinese+(float)p->english+(float)p->math;
p->aver=((float)p->chinese+(float)p->english+(float)p->math)/3.0;
if(head=NULL)head=p;
else q->next=p;
q=p;
look(p);
p=(struct student*)malloc(LEN);
cout<<"姓名:"<<endl;
cin>>nam;
total++;
}
p->next=NULL;
}
void look(struct student *stu)
{
struct student *p;
p=stu;
cout<<"姓名:"<<p->name<<" "<<"学号:"<<p->num<<" "<<
"数学:"<<p->math<<" "<<"外语:"<<p->english<<" "
<<"语文:"<<p->chinese<<" "<<"总分:"<<p->sum<<" "
<<"平均分:"<<p->aver<<endl;
}
void lookall()
{
struct student *p;
p=head;
while(p)
{
look(p);
p=p->next;
}
}
void add()
{
struct student *p,*q;
p=head;
q=(struct student *)malloc(LEN);
cout<<"姓名:"<<endl;
cin>>p->name;
cout<<"学号:"<<endl;
cin>>p->num;
cout<<"数学成绩:"<<endl;
cin>>p->math;
cout<<"外语成绩:"<<endl;
cin>>p->english;
cout<<"语文成绩:"<<endl;
cin>>p->chinese;
p->sum=(float)p->chinese+(float)p->english+(float)p->math;
p->aver=((float)p->chinese+(float)p->english+(float)p->math)/3.0;
while(p)
{
p=p->next;
}
{p=q;q->next=NULL;
}
total++;
}
void query_by_name()
{
struct student *p;
char na[20];
int flag=0;
p=head;
cout<<"请输入查找的姓名:"<<endl;
cin>>na;
if(p==NULL)cout<<"无记录"<<endl;
else
while(p)
{ if(!strcmp(p->name,na)){look(p);flag=1;}
else p=p->next;
}
if(flag=0)cout<<"没有找到"<<endl;
}
void query_by_num()
{
struct student *p;
int i;
p=head;int flag=0;
cout<<"请输入查找的学号:"<<endl;
cin>>i;
if(p==NULL)cout<<"无记录"<<endl;
else
while(p);
{
if(p->num==i){look(p);flag=1;}
else p=p->next;
}
if(flag=0)cout<<"没有找到"<<endl;
}
void del()
{
struct student*p,*q;
p=head;
int num;
cout<<"输入删除的学号"<<endl;
cin>>num;
while((p->num!=num)&&(p->next!=NULL))
{
q=p;
p=p->next;
}
if(p->num==num)
{
if(p=head)
head=p->next;
else
q->next=p->next;
cout<<"删除掉的学生是:"<<endl;
look(p);
total--;
}
else cout<<"没有找到"<<endl;
}