Home > Software engineering >  How to scan multiple strings in one place?
How to scan multiple strings in one place?

Time:04-05

I am working on a project. I need to get each student's courses and their number, for example:

Lisa Miller  890238
Mathematics  MTH345
Physics  PHY357  
ComputerSci  CSC478  
History  HIS356  

I do not know how to register all those courses and their numbers in one place specified for that student. What should I do?

CodePudding user response:

It looks like you need two structures:

One for the student and their list of courses, and one for a course.

Assuming you know maximum number of courses a student can take:

struct course {
    char name[MAX_COURSE_NAME_LEN];
    char id[MAX_COURSE_ID_LEN]; //this one should be 7
};

struct student {
    char name[MAX_STUDENT_NAME_LEN];
    unsigned int id; //student ids are all numbers, right?
    int num_courses; //number of courses student actually takes
    struct course courses[MAX_COURSES];
}

If you want to be memory efficient or the maximum number of courses a student can take is not specified, you should used a linked list of courses instead of an array.

You will need custom code to read this from file and write it back to a file.

C standard library does not come with serialization and deserialization functions, but if this is more than a homework project, you may want to look for a dedicated serialization library.

  •  Tags:  
  • c
  • Related