问题:输入字符串后出现死循环:
学生成绩管理系统,“Search_by_name”函数中:
printf("请输入目标学生姓名:");
scanf("%s",xingming);
一部分,输入任意字符串(如“gfds""bgfdx")会出现死循环,循环出现菜单界面(循环调用caidan()函数),但输入数字运行正常。
为保证提问可读性,已略去无关函数。
麻烦各位指正,非常感谢!
(使用编程软件:DEV-C++,系统win11)
源代码:
#include<stdio.h>
#include<string.h>
#include <stdlib.h>
//变量区
//定义结构体:
typedef struct student
{
char name[30]; //名字
int xuehao; //学号
int score[6]; //成绩
int zongfen; //个人总分
int pingjunfen; //个人平均分
}student;
int main()
{
//函数声明区
int caidan();//菜单函数
int Input_record(struct student stu[],int banpingjunfen[],int banzongfen[],int kemushu_xueshengshu[2]);//录入成绩
int Calculate_total_and_average_score_of_every_course(struct student stu[],int banpingjunfen[],int banzongfen[],int kemushu,int xueshengshu);//计算每门课程的总分和平均分
int Calculate_total_and_average_score_of_every_student(struct student stu[],int banpingjunfen[],int banzongfen[],int kemushu,int xueshengshu);//计算每个学生的总分和平均分
int Sort_in_descending_order_by_total_score_of_every_student(struct student stu[],struct student stu1[],int kemushu,int xueshengshu);//按每个学生的总分由高到低排出名次表;
int Sort_in_asending_order_by_total_score_of_every_student(struct student stu[],struct student stu1[],int kemushu,int xueshengshu);//按每个学生的总分由低到高排出名次表;
int Sort_in_ascending_order_by_number(struct student stu[],struct student stu1[],int kemushu,int xueshengshu);//按学号由小到大排出成绩表;
int Sort_in_dictionary_order_by_name(struct student stu[],struct student stu1[],int kemushu,int xueshengshu);//按姓名的字典顺序排出成绩表;
int Search_by_number(struct student stu[],struct student stu1[],int kemushu,int xueshengshu);//按学号查询学生排名及其各科考试成绩;
int Search_by_name(struct student stu[],struct student stu1[],int kemushu,int xueshengshu);//按姓名查询学生排名及其各科考试成绩;
int Statistic_analysis(struct student stu[],int kemushu,int xueshengshu);//等级统计
int List_record(struct student stu[],int kemushu,int xueshengshu,int banzongfen[],int banpingjunfen[]);//输出每个学生的所有信息
int Write_to_a_file(struct student stu[],int kemushu,int xueshengshu,int banzongfen[],int banpingjunfen[]);//l将每个学生的记录信息写入文件;
int Read_from_a_file(struct student stu[],int kemushu,int xueshengshu,int banzongfen[],int banpingjunfen[]);//从文件中读出每个学生的记录信息并显示。
//变量设定区
student stu[30];
student stu1[30];
int banpingjunfen[6]; //班平均分
int banzongfen[6]; //班总分
int kemushu_xueshengshu[2] = {0};//使得学生数、科目数能同时反出来 0位:科目数;1位:学生数
int moshi;
int kemushu = 0,xueshengshu = 0;
int c = 0;
//正式使用区
while(1)
{
moshi = caidan(); //调用“菜单模块”
switch (moshi)
{
case 1:
Input_record(stu,banpingjunfen,banzongfen,kemushu_xueshengshu);
kemushu = kemushu_xueshengshu[0];
xueshengshu = kemushu_xueshengshu[1];
break;
case 2:
Calculate_total_and_average_score_of_every_course(stu,banpingjunfen,banzongfen,kemushu,xueshengshu);
break;
case 3:
Calculate_total_and_average_score_of_every_student(stu,banpingjunfen,banzongfen,kemushu,xueshengshu);
break;
case 4:
Sort_in_descending_order_by_total_score_of_every_student(stu,stu1,kemushu,xueshengshu);
break;
case 5:
Sort_in_asending_order_by_total_score_of_every_student(stu,stu1,kemushu,xueshengshu);
break;
case 6:
Sort_in_ascending_order_by_number(stu,stu1,kemushu,xueshengshu);
break;
case 7:
Sort_in_dictionary_order_by_name(stu,stu1,kemushu,xueshengshu);
break;
case 8:
Search_by_number(stu,stu1,kemushu,xueshengshu);
break;
case 9:
Search_by_name(stu,stu1,kemushu,xueshengshu);
c = 0;
break;
case 10:
Statistic_analysis(stu,kemushu,xueshengshu);
break;
case 11:
List_record(stu,kemushu,xueshengshu,banzongfen,banpingjunfen);
break;
case 12:
Write_to_a_file(stu,kemushu,xueshengshu,banzongfen,banpingjunfen);
break;
case 13:
Read_from_a_file(stu,kemushu,xueshengshu,banzongfen,banpingjunfen);
break;
case 0:
c = 1;
break;
}
if(c == 1)
break;
}
}
int caidan()//菜单函数
{
int x;//模式
printf("欢迎使用成绩系统,请通过输入数字选择操作:\n") ;
printf("1. Input record\n");
printf("2. Calculate total and average score of every course\n");
printf("3. Calculate total and average score of every student\n");
printf("4. Sort in descending order by total score of every student\n");
printf("5. Sort in asending order by total score of every student\n");
printf("6. Sort in ascending order by number\n");
printf("7. Sort in dictionary order by name\n");
printf("8. Search by number\n");
printf("9. Search by name \n");
printf("10.Statistic analysis \n");
printf("11.List record\n");
printf("12.Write to a file\n");
printf("13.Read from a file\n");
printf("0. Exit\n");
printf("Please enter your choice:");
scanf("%d",&x);
return x;
}
int Search_by_name(struct student stu[],struct student stu1[],int kemushu,int xueshengshu)//按姓名查询学生排名及其各科考试成绩;
{
//排序阶段
Sort_in_descending_order_by_total_score_of_every_student(stu,stu1,kemushu,xueshengshu);//按每个学生的总分由高到低排出名次表;
//输入目标姓名
char xingming[30];
printf("请输入目标学生姓名:");
scanf("%s",xingming);
//查询、输出阶段
int xuesheng = 0,kemu = 0,chenggong = 0;
for(xuesheng=0;xuesheng<xueshengshu;xuesheng++)
if(strcmp(xingming,stu1[xuesheng].name)==0)
{
printf("查询成功,学生%s的排名为%d,各科成绩分别为:\n",stu1[xuesheng].name,xuesheng+1);
for(kemu=0;kemu<kemushu;kemu++)
printf("第%d科:%d分\n",kemu+1,stu1[xuesheng].score[kemu]);
chenggong = 1;
}
if(chenggong == 0)
{
printf("查无此人,请检查输入姓名。\n");
}
return 0;
}