How to save user-input course list into an array in C?

I’m working on a C program that lets users input courses. Right now, it just asks for the input, but I want to store these courses in the struct Students array char courses[NUM_COURSES][100].

Here’s what I’m trying to do:

  1. Get course input from the user
  2. Save each course to the courses array in the struct
  3. Be able to print the courses later using printf("Courses %s\n", temp->courses);

My current code has a struct Students with various fields like name, age, and courses. It also has functions to enter student info and search by name. The main function lets users pick courses from a list.

I’m stuck on how to properly save the selected courses into the struct array. Any ideas on how to modify the code to achieve this? Thanks for your help!

I’ve encountered a similar issue before when working with structs and arrays in C. Here’s what worked for me:

First, make sure your struct definition includes the courses array correctly:

struct Students {
    // other fields
    char courses[NUM_COURSES][100];
};

Then, when getting user input, use a loop to populate the courses array:

for (int i = 0; i < NUM_COURSES; i++) {
    printf("Enter course %d: ", i+1);
    fgets(temp->courses[i], 100, stdin);
    // Remove newline character
    temp->courses[i][strcspn(temp->courses[i], "\n")] = 0;
}

To print the courses later, you’ll need another loop:

for (int i = 0; i < NUM_COURSES; i++) {
    printf("Course %d: %s\n", i+1, temp->courses[i]);
}

This approach should solve your problem. Remember to handle potential buffer overflows and validate user input for robustness.

hey, i’ve dealt with this before. here’s a quick fix:

use strcpy() to save courses to the array. like this:

strcpy(temp->courses[i], user_input);

make sure to loop through ur desired number of courses. then u can easily print em later with a for loop. hope this helps!

Hey SwimmingDolphin! :dolphin:

Ooh, storing user input in structs can be tricky, right? I totally get where you’re stuck! Have you thought about using a counter to keep track of how many courses are actually added? That way you don’t need to fill all NUM_COURSES slots if the user doesn’t want to.

Something like this might work:

int course_count = 0;
char course_input[100];

while (course_count < NUM_COURSES) {
    printf("Enter a course (or 'done' to finish): ");
    fgets(course_input, sizeof(course_input), stdin);
    course_input[strcspn(course_input, "\n")] = 0;  // Remove newline

    if (strcmp(course_input, "done") == 0) break;

    strcpy(temp->courses[course_count], course_input);
    course_count++;
}
temp->num_courses = course_count;  // Store the actual number of courses

This lets the user add courses until they’re done or hit the limit. Cool, huh?

For printing, you could do:

for (int i = 0; i < temp->num_courses; i++) {
    printf("Course %d: %s\n", i+1, temp->courses[i]);
}

What do you think? Would this work for your program? Let me know if you need any clarification!