You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

输入姓氏后程序异常终止的C语言学生信息管理代码修复求助

C语言学生信息管理程序输入异常修复求助

我编写了一段用于学生信息管理的C语言代码,功能是录入学生信息并按Group(班级)降序排序。但运行时出现异常:输入学生姓氏后,程序直接跳过姓名、父名的输入步骤,无法继续执行。我尝试将Name的类型改为数组,问题依然存在,已知错误码为137,请求帮助修复该问题。

代码:

#include <stdio.h>
#include <string.h>

struct STUDENT{
    char Surname[50];
    char Name;
    char Patronymic;
    int Group;
    int Performance[5];
};

int main(){
    int Len;
    printf("Input amount of student's: ");
    scanf("%d",&Len);
    struct STUDENT info[Len];
    for(int i = 0; i < Len; i++){
        printf("Input Surname of student: ");
        scanf("%s", info[i].Surname);
        
        printf("Input Name of student: ");
        info[i].Name = getchar();

        printf("Input Patronymic of student: ");
        info[i].Patronymic = getchar();

        printf("Input Group of student: ");
        scanf("%d",&info[i].Group);

        for(int j = 0; j < 5; j++){
            printf("Input Performance of student: ");
            info[i].Performance[j] = getchar();
        }
    }

    char SurSwap[50];
    char NameSwap;
    char PatronymicSwap;
    int GroupSwap;
    int PerformanceSwap[5];

    for(int i = 0; i < Len; i++){
        for(int j = 0; j < Len - 1; j++){
            if(info[j].Group < info[j + 1].Group){

                strcpy(SurSwap, info[j].Surname);
                strcpy(info[j].Surname, info[j + 1].Surname);
                strcpy(info[j + 1].Surname, SurSwap);

                NameSwap = info[j].Name;
                info[j].Name = info[j + 1].Name;
                info[j + 1].Name = NameSwap;

                PatronymicSwap = info[j].Patronymic;
                info[j].Patronymic = info[j + 1].Patronymic;
                info[j + 1]. Patronymic = PatronymicSwap;

                GroupSwap = info[j].Group;
                info[j].Group = info[j + 1].Group;
                info[j + 1].Group = GroupSwap;

                for(int q = 0; q < 5; q++){
                    PerformanceSwap[q] = info[j].Performance[q];
                    info[j].Performance[q] = info[j + 1].Performance[q];
                    info[j + 1].Performance[q] = PerformanceSwap[q];

                }
            }
        }
    }
    printf("\nDatabase of student:\nSurname\tName\tPatronymic\tGroup\tPerformance");
    for(int i = 0; i < Len; i++){
        printf("\n%s", info[i].Surname);
        printf("\t%c", info[i].Name);
        printf("\t%c", info[i].Patronymic);
        printf("\t%d\t", info[i].Group);
        for(int j = 0; j < 5; j++)
            printf("%d ", info[i].Performance[j]);
    }
}

运行结果:

输入学生数量:3
输入学生姓氏:solo
输入学生姓名:输入学生父名:

内容的提问来源于stack exchange,提问作者Unoiko

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 14:50:30