一个没有C币的萌新,想问问大佬们一些关于程序停止运行的小问题
下面是我敲的一段代码,但是总是在进行插入和显示所有这两项功能时,程序停止运行,我想问下这是为什么啊?
#include<iostream.h>
#include<string>
typedef struct student
{
int id;
float score;
char name[10];
student *next;
}stu;
stu *head=NULL;
void record();
void insert();
void delt();
void locate();
void print();
int main()
{
int c,i=1;
cout<<"1、输入学生成绩 2、插入 3、删除 4、查找 5、显示所有 0、退出"<<endl;
cin>>c;
do
{
switch(c)
{
case 0: i=0; break;
case 1: record(); break;
case 2: insert(); break;
case 3: delt(); break;
case 4: locate(); break;
case 5: print(); break;
default: cout<<"输入有误!"<<endl; break;
}
cout<<"输入选择(0-7): ";
cin>>c;
}while(c!=0);
return 0;
}
void record()
{
stu *p,*q;
int i,n;
cout<<"输入学生人数:";
cin>>n;
i=0;
cout<<"输入学生信息:姓名 学号 成绩"<<endl;
while(i<n)
{
p=new stu;
if(head==NULL)
{
head=p;
q=head;
}
else
{
cin>>p->name>>p->id>>p->score;
q->next=p;
q=p;
i++;
}
}
}
void insert()
{
stu *p=head, *s=NULL,*q=NULL;
int x,count=0;
cout<<"插入的位置为:";
cin>>x;
p=q->next;
while(p!=NULL && count<x-1)
{
q=p;
p=q->next;
count++;
}
if(p=NULL) cout<<"位置错误";
else
{
cout<<"请输入插入学生的信息:姓名 学号 成绩"<<endl;
s=new stu;
cin>>p->name>>p->id>>p->score;
s->next=p->next;
p->next=s;
}
}
void delt()
{
stu *p,*q;
int id,i=1;
cout<<"输入要删除学生的学号: ";
cin>>id;
q=head;
p=q->next;
while(i)
{
if(p->id==id)
{
q->next=p->next;
delete p;
i=0;
break;
}
q=p;
p=q->next;
}
}
void locate()
{stu *p,*q;
int id,i=1;
cout<<"输入要查找学生的学号: ";
cin>>id;
q=head;
p=q->next;
while(i)
{
if(p->id==id)
{
cout<<p->name<<","<<p->score<<endl;
i=0;
}
q=p;
p=q->next;
}
}
void print()
{
stu *p;
p=head->next;
while(p)
{
cout<<p->name<<","<<p->score<<endl;
p=p->next;
}
}