运行报错问题。。。求解
题目如下:
假设有3名学生信息,包括姓名、性别、出生年月、总分等。从键盘输入相关信息,并输出。 (使用结构指针)
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
struct stu
{
string name;
string sex;
short bthyear;
short bthmonth;
short bthday;
int score;
stu *next;
};
stu *create()
{
stu *head;
head=NULL;
stu *now;
stu *end;
now=new stu;
cin >> now->name;
cin >> now->sex;
cin >> now->bthyear >> now->bthmonth >> now->bthday;
cin >> now->score;
end=now;
for (int i=0;i<=1;i++)
{
if (head==NULL)
head=end;
else
end->next=now;
end=now;
now=new stu;
cin >> now->name;
cin >> now->sex;
cin >> now->bthyear >> now->bthmonth >> now->bthday;
cin >> now->score;
}
end->next=NULL;
delete now;
return head;
}
void showDetail(stu *head)
{
stu *p;
p=NULL;
p=head;
for (int i=0;i<=2;i++)
{
cout << p->name << endl;
cout << p->sex << endl;
cout << p->bthyear << '-' << p->bthmonth << '-' << p->bthday << endl;
cout << p->score << endl;
p=p->next;
}
}
int main()
{
stu *head;
head=create();
showDetail(head);
system ("pause");
return 0;
}
写了这些代码,编译能通过但运行时报错。。。求解。。。