简体   繁体   English

结构数组的用户输入 C

[英]User Input to Array of Structs C

I am creating a program that gets user input and puts it into a struct called Student before returning back to a main menu.我正在创建一个程序,它获取用户输入并将其放入一个名为 Student 的结构中,然后返回主菜单。 From the main menu you can add another student.从主菜单中,您可以添加另一个学生。 This works but when I try to print all of the Students data that has been added to the array of structs it only prints the last Student that was added.这可行,但是当我尝试打印已添加到结构数组中的所有学生数据时,它只打印添加的最后一个学生。

Here is the struct:这是结构:

 struct Student
{
    char* firstName;
    int age, cuid;
    float GPA;
};

This is how I'm allocating space for the array in main:这就是我在 main 中为数组分配空间的方式:

struct Student* studentArray = malloc(*recordMaxPtr * sizeof(struct Student));

Here are the functions that capture user input and print the array:以下是捕获用户输入并打印数组的函数:

// Gets info and adds student to the array of structs
void addStudents(struct Student* s, int *numStudents)
{
    // Allocate memory for the first name
    char *firstName = (char*) malloc(25*sizeof(char));

    // Gets the name of student
    printf("What is the name of the student you'd like to add? ");
    gets(s->firstName);

    printf("\n"); // blank line

    // Gets age of student
    printf("How old is the student? ");
    scanf("%d", &s->age);

    printf("\n"); // blank line

    // Gets CUID
    printf("What is his / her CUID number? ");
    scanf("%d", &s->cuid);

    printf("\n"); // blank line

    // Gets GPA
    printf("What is the student's GPA? ");
    scanf("%f", &s->GPA);

    printf("\n"); // blank line
}

// Sorts the array and then lists all of the saved students
void printStudents(struct Student* s, int *numStudents)
{
    //bsort();

    for (int i = 0; i < *numStudents; i++)
    {
        printf("Student #%d\n", i + 1);
        printf("                Student's Name: %s\n", s[i].firstName);
        printf("                Age: %d\n", s[i].age);
        printf("                CUID: %d\n", s[i].cuid);
        printf("                GPA: %2f\n", s[i].GPA);
    }
}

I thought about trying to use a for loop to gather all of the student's data that I need at once but this is a school project and I'm not allowed to do it that way.我曾想过尝试使用 for 循环来一次收集我需要的所有学生数据,但这是一个学校项目,我不允许这样做。 I've been trying to figure this out for a while and I've sort of hit a brick wall on what to do.我一直试图弄清楚这一点,但我在做什么时遇到了障碍。 My best guess is that data is getting overwritten every time you enter a new student to the array but I'm not sure how to fix that.我最好的猜测是,每次您将新学生输入数组时,数据都会被覆盖,但我不知道如何解决这个问题。

You allocated a memory for the first name, but didn't save that anywhere.您为名字分配了 memory,但没有将其保存在任何地方。 You have to store the address to the structure.您必须将地址存储到结构中。

    // Allocate memory for the first name
    char *firstName = (char*) malloc(25*sizeof(char));
    s->firstName = firstName; // add this

Also here are some more things to do for better code:为了获得更好的代码,还有一些事情要做:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM