我必须用链表制作学生数据库,但是在输入第一个学生的信息后,我不能输入其他学生的名字。
struct Student
{
char name[20];
int egn;
struct Student *next;
}*start=NULL;
void creat()
{
char ch;
do
{
struct Student *newStudent,*current;
newStudent=(struct Student *)malloc(sizeof(struct Student));
printf("nEnter student's name: ");
gets(newStudent->name);
printf("Enter student's egn: ");
scanf("%d",&newStudent->egn);
newStudent->next=NULL;
if(start==NULL)
{
start=newStudent;
current=newStudent;
}
else
{
current->next=newStudent;
current=newStudent;
}
printf("nDo you want to creat another : ");
ch=getche();
}while(ch!='n');
}
和结果:
Enter student's name: First Student
Enter student's egn: 234234
Do you want to creat another : y
Enter student's name: Enter student's egn: 23452342
Do you want to creat another : y
Enter student's name: Enter student's egn: 234234
Do you want to creat another : n
我错在哪里?
尝试在gets(newStudent->name)之前和之后使用fflush(stdin);声明