我必须使用链接列表来建立学生数据库,但是在输入有关第一个学生的信息之后,我无法输入彼此的名字。

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);声明

09-27 08:05