xdtcfvgbuh 2023-04-18 09:51 采纳率: 100%
浏览 27
已结题

问题:输入字符串后出现死循环

问题:输入字符串后出现死循环:
学生成绩管理系统,“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;
}

img

  • 写回答

4条回答 默认 最新

  • qzjhjxj 2023-04-18 11:52
    关注

    修改如下,供参考:

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <conio.h>      // 修改
    
    //变量区
    //定义结构体:
    typedef struct student
    {
        char name[30];  //名字 
        int xuehao;     //学号 
        int score[6];   //成绩 
        int zongfen;    //个人总分 
        int pingjunfen;  //个人平均分 
    }student;
    
    //函数声明区                                  //以下修改,函数声明,变量声明 应移动到主函数外面,作用域为全局
    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 kemushu = 0, xueshengshu = 0;
    
    
    int main()
    {
        int c = 0;
        int moshi;
        //正式使用区
        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;
            printf("按任意键继续..."); // 修改
            _getch();                  // 修改
        }
    }
    
    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;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 4月26日
  • 已采纳回答 4月18日
  • 创建了问题 4月18日

悬赏问题

  • ¥15 微信购物商城购物车的所有功能以及购物车,和首页的搜索功能
  • ¥40 servlet的web程序部署出错
  • ¥50 activiti 新建流程系列问题
  • ¥50 为什么我版本升级之后运行速度变慢5倍??
  • ¥15 如何在gazebo中加载机械臂和机械手
  • ¥15 纯运放实现隔离采样方案设计
  • ¥20 easyconnect无法连接后缀带.com的网站,一直显示找不到服务器ip地址,但是带有数字的网站又是可以连接上的,如何解决
  • ¥15 电脑开机过商标后就直接这样,求解各位
  • ¥15 mysql , 用自己创建的本地主机和用户名 登录不上
  • ¥15 关于#web项目#的问题,请各位专家解答!