gpt4 book ai didi

c - 在链表中输入 char 变量

转载 作者:行者123 更新时间:2023-11-30 17:33:31 25 4
gpt4 key购买 nike

我必须使用链接列表创建学生数据库,但是在输入第一个学生的信息后,我无法输入其他每个学生的姓名。

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

关于c - 在链表中输入 char 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23723352/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com