函数仅覆盖第一个学生的学生信息

问题描述 投票:0回答:1

当我尝试按要求输入学生详细信息时,它会获取第二个课程详细信息并将其复制到第一个课程中。这仅发生在第一个学生输入时。在获取第一个学生课程信息后,效果完全正常

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#define MAX_NAME_LEN 22
#define MAX_COURSES 10
#define FIRST_NAME 11
#define LAST_NAME 11
#define Student_Amount 3
#define ALLSTUDENTSTRING 69
typedef struct Course_info
{
    int courseNum;
    int grade;
} COURSE_INFO;

typedef struct Student
{
    char name[MAX_NAME_LEN];
    int identity;
    int nofCourses;
    COURSE_INFO course_info[MAX_COURSES];
} STUDENT;

struct Course_info getCourseInfo(COURSE_INFO course_info)
{
    printf("Please enter your course number and grade: ");
    scanf("%d %d", &course_info.courseNum, &course_info.grade);
    
    return course_info;
}
struct Student getStudentInfo()
{
    struct Student student;
    int stringCounter = 0;
    char FirstName[FIRST_NAME];
    char LastName[LAST_NAME];
    printf("Enter student first name and last name(seperated by spaces) :");
    scanf(" %s", FirstName);
    strcpy(student.name, FirstName);
    scanf(" %s", &LastName);
    strcat(student.name, " ");
    strcat(student.name, LastName);

    
    printf("Please enter the student's ID: ");
    scanf("%d", &student.identity);

    printf("Enter number of courses taken: ");
    scanf("%d", &student.nofCourses);
    for (int i = 0; i < student.nofCourses; i++)
    {
        student.course_info[i] = getCourseInfo(student.course_info[i]);
    }
    return student;
}


void sortCourses(COURSE_INFO data[], int size)
{
    int i, j,temp;
    for ( i = 0; i < size-1; i++)
    {
        for ( j = 0; j < size - i - 1; j++)
        {
            if (data[j].courseNum > data[j+1].courseNum)
            {
                temp = data[j+1].courseNum;
                data[j].courseNum=data[j + 1].courseNum;
                data[j + 1].courseNum = temp;
            }
        }
    }
}

int getStudentNames(STUDENT stuData[], int size, int courseNum, char stuNames[])
{
    int studentCount = 0;
    int i;
    for (i = 0; i < size; i++)
    {
        for (int j = 0; j < stuData[i].nofCourses; j++)
        {
            if (courseNum == stuData[i].course_info[j].courseNum)
            {
                strcat(stuNames, stuData[i].name);
                strcat(stuNames, " ");
                studentCount++;
                break;
            }
        }
    }
    printf("%s", stuNames);
    while (!strcmp(stuNames[i],"\0"))
    {
        if (i%2==0)
        {
            printf("\n");
        }
        printf("%c", stuNames[i]);
        if (strcmp(stuNames[i]," "))
        {
            i++;
        }
        
    }

    return studentCount;

}

假设我输入了课程号和成绩,当我输入第一个学生的第二个课程信息时,我的第一个课程信息变成了第一个和第二个

void main()
{
    int courseNum;
    char StuNames[ALLSTUDENTSTRING]="";
    printf("Welcome students!! \n");
    printf("and bye bye Pizzera \n");
    struct Student student_data[Student_Amount];
    for (int i = 0; i < Student_Amount; i++)
    {
        student_data[i] = getStudentInfo();
        
    }
     scanf("%d", &courseNum);
     printf("Names of students in course #%d", courseNum);
     getStudentNames(student_data,Student_Amount,courseNum,StuNames);
}

这些是我遇到问题的输入:

输入

Lois Noir
13345
2
1334 89
1252 92
Gur Lex
12587
2
1334 77
1445 81
Lois Lois
16577
1
1334 68
1334

第一个学生的课程编号不是 1334,年级 92,而是他的两门课程都是 1252,年级 92。

c struct
1个回答
0
投票

您的代码存在多个问题,评论中提到了一些问题:

  1. 失踪了
    #include <string.h>
  2. strcmp(stuNames[i], "\0")
    错误,应替换为
    (stuNames[i] == 0)
  3. scanf(" %s", &LastName);
    &
    是额外的
  4. 您的最后一个功能:

您可以执行以下操作,而不是添加空格,然后用换行符替换它们:

strcat(stuNames, stuData[i].name);
strcat(stuNames, "\n");

// AFTER LOOP//
stuNames[strlen(stuNames - 1)] = 0;  // put terminating character to the end of string

额外

我建议更改此行:

student.course_info[i] = getCourseInfo(student.course_info[i]);
getCourseInfo(&student.course_info[i]);

函数所在位置:

void getCourseInfo(COURSE_INFO *course_info)
{
    printf("Please enter your course number and grade: ");
    scanf("%d %d", &course_info->courseNum, &course_info->grade);
}
© www.soinside.com 2019 - 2024. All rights reserved.