0

I am creating a program that gets user input and puts it into a struct called Student before returning back to a main menu. 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:

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. 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.

Drake Owen
  • 87
  • 5
  • The `s->` in `addStudent` really is the same as the `s[0].` in `print_student`. That means that you always overwrite the data for the first student. You should either pass an additional index or a pointer to a particular student, e.g. `&s[i]` to `add_student`. – M Oehm Feb 05 '21 at 17:43
  • The `numStudents` argument is never used in your `addStudents` function. Probably you need to increment the value of `numStudents`. – Antonio Feb 05 '21 at 18:18

1 Answers1

0

You allocated a memory for the first name, but didn't save that anywhere. 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:

  • Check if malloc() succeeded
  • Check if scanf() succeeded
  • Replace gets() with fgets() and removal of newline character (gets() has unavoidable risk of buffer overrun and removed from new C specification)
  • Remove the cast of result of malloc() (c - Do I cast the result of malloc? - Stack Overflow)
MikeCAT
  • 73,922
  • 11
  • 45
  • 70