标题:程序运行时出现内存错误,怎么回事
只看楼主
想飞
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2014-5-14
结帖率:50%
已结贴  问题点数:10 回复次数:4 
程序运行时出现内存错误,怎么回事
#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;
}
搜索更多相关主题的帖子: english include insert 
2014-05-17 13:35
砖家的谎言
Rank: 12Rank: 12Rank: 12
等 级:禁止访问
威 望:30
帖 子:693
专家分:3898
注 册:2013-12-6
得分:4 
你这个是C++的

我不是砖家,要努力成为砖家。
2014-05-18 16:15
猪鹿蝶1234
Rank: 1
等 级:新手上路
帖 子:6
专家分:4
注 册:2013-10-1
得分:4 
void type()
{
...
if(head=NULL)改为if(head= =NULL)
...
}
2014-05-21 23:10
yangfrancis
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:贵宾
威 望:141
帖 子:1510
专家分:7661
注 册:2014-5-19
得分:4 
if(flag=0)cout<<"没有找到"<<endl;
这一行的条件语句也应该是双等号吧,一个等号好像是VB的语法了。
2014-05-22 11:03
yangfrancis
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:贵宾
威 望:141
帖 子:1510
专家分:7661
注 册:2014-5-19
得分:0 
del()块中if(p=head)也存在同样的问题
2014-05-22 11:04



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-431672-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.040717 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved