自己写的一段代码,编译正常,运行出错……求解……
#include<iostream>
#include<string>
using namespace std;
struct student
{
short unsigned int num;
string name;
float score[3];
student *next;
};
void creat(void)
{
int i,s;
cout<<"请输入学生总数:";
cin>>s;
student *p;
cout<<"现在开始按学号顺序建立学生档案"<<endl<<endl;
for(i=1;i<=s;i++)
{
cout<<"现在输入"<<i<<"号学生姓名:";
cin>>p->name;
(p->next)=new student;
p=p->next;
}
}
int main(void)
{
creat();
return 1;
}
上面代码编译运行,输入总数正常,输入第一个学生的姓名之后,敲回车,程序卡死,结束程序,然后是下面的情况:
请输入学生总数:50
现在开始按学号顺序建立学生档案
现在输入1号学生姓名:ggg
--------------------------------
Process exited with return value 3221225477
Press any key to continue . . .
额……那啥,LZ是自学的,看的老谭的书,自己看不出上面代码哪里有问题,求赐教
[解决办法]
试试这样呢。
p=new student;
cin>>p->name;
p=p->next;
另外,你还得弄个指针记录链表头部。