Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdbool.h>
- #include <stdio.h>
- #define MAXSTUDENTS 15
- typedef struct {
- int crn;
- int hours;
- char* prefix;
- } course_t;
- typedef struct {
- char name[50];
- int id;
- int nCourses;
- course_t courses[4];
- struct student* next;
- } student_t;
- student_t students[MAXSTUDENTS]; // make array of students
- void addStudent(void) {
- student_t new;
- for (int c = 0; c < 4; c++) new.courses[c].crn = 0;
- int tempIDstorage;
- printf("\nEnter the student's id: ");
- scanf("%d", &tempIDstorage);
- getchar();
- for (int c = 0; c < MAXSTUDENTS; c++)
- if (students[c].id == tempIDstorage) {
- printf("\nSorry, a student with that ID already exists!\n");
- return;
- }
- new.id = tempIDstorage;
- printf("\nEnter student's name: ");
- fgets(new.name, sizeof(new.name), stdin);
- new.name[strcspn(new.name, "\n")] = 0;
- printf("\nEnter how many courses [%s] is taking (up to 4)?\n\t", new.name);
- scanf("%d", &(new.nCourses));
- while (new.nCourses < 0 || new.nCourses > 4) {
- printf("Invalid input! Try again: ");
- scanf("%d", &(new.nCourses));
- }
- if (new.nCourses == 0) goto end;
- char yn;
- printf("\nDisplay list of courses? Y = yes, anything else = no\n\t");
- scanf(" %c", &yn);
- fflush(stdin);
- if (yn == 'y' || yn == 'Y') printCourseList();
- printf("\nEnter the student's %d course number(s):\n\t", new.nCourses);
- for (int c = 0; c < new.nCourses; c++) {
- scanf("%d", &(new.courses[c].crn));
- if (!crnCorrect(new.courses[c].crn)) {
- printf("Invalid crn(s)!");
- return;
- }
- }
- end:
- for (int c = 0; c < MAXSTUDENTS; c++) {
- if (students[c].id == 0) students[c] = new;
- printf("\nStudent added successfully!\n");
- return;
- }
- printf("\nSorry - maximum number of students (%d) reached. \n", MAXSTUDENTS);
- printf(" Student not added\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement