我必须使用链表创建学生数据库,但在输入第一名学生的信息后,我无法输入其他人的姓名。
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
我的错误在哪里?
答案 0 :(得分:0)
尝试在获取之前和之后使用fflush(stdin)(newStudent-> name);声明