int find_student_name(struct class_room* PIC) { int i; char name[20]; // 修改为字符串类型 printf('请输入要查找的学生姓名:\n'); // 修改提示语句 scanf('%s', name); // 修改输入方式 for (i = 0; i < PIC->n; i++) // 修改起始位置为0 { if (strcmp(name, PIC->st[i].name) == 0) // 修改比较方式为字符串比较 { printf('查有此人\n'); printf('the name is %s\n', PIC->st[i].name); printf('the age is %d\n', PIC->st[i].age); printf('the sex is %s\n', PIC->st[i].sex); printf('the birth is %s\n', PIC->st[i].birth); printf('the address is %s\n', PIC->st[i].address); printf('the phone is %s\n', PIC->st[i].phone); return i; // 查找到后立即返回 } } printf('查无此人\n'); // 修改输出位置 continue_2(PIC); return -1; // 返回-1表示未查找到 }

C语言学生信息查找代码优化:使用字符串比较和错误处理

原文地址: https://www.cveoy.top/t/topic/oR8n 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录