Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. #include <stdbool.h>
  2. #include <stdio.h>
  3. #define MAXSTUDENTS 15
  4.  
  5. typedef struct {
  6. int crn;
  7. int hours;
  8. char* prefix;
  9. } course_t;
  10. typedef struct {
  11. char name[50];
  12. int id;
  13. int nCourses;
  14. course_t courses[4];
  15. struct student* next;
  16. } student_t;
  17. student_t students[MAXSTUDENTS]; // make array of students
  18.  
  19. void addStudent(void) {
  20. student_t new;
  21. for (int c = 0; c < 4; c++) new.courses[c].crn = 0;
  22. int tempIDstorage;
  23. printf("\nEnter the student's id: ");
  24. scanf("%d", &tempIDstorage);
  25. getchar();
  26. for (int c = 0; c < MAXSTUDENTS; c++)
  27. if (students[c].id == tempIDstorage) {
  28. printf("\nSorry, a student with that ID already exists!\n");
  29. return;
  30. }
  31. new.id = tempIDstorage;
  32. printf("\nEnter student's name: ");
  33. fgets(new.name, sizeof(new.name), stdin);
  34. new.name[strcspn(new.name, "\n")] = 0;
  35. printf("\nEnter how many courses [%s] is taking (up to 4)?\n\t", new.name);
  36. scanf("%d", &(new.nCourses));
  37. while (new.nCourses < 0 || new.nCourses > 4) {
  38. printf("Invalid input! Try again: ");
  39. scanf("%d", &(new.nCourses));
  40. }
  41. if (new.nCourses == 0) goto end;
  42. char yn;
  43. printf("\nDisplay list of courses? Y = yes, anything else = no\n\t");
  44. scanf(" %c", &yn);
  45. fflush(stdin);
  46. if (yn == 'y' || yn == 'Y') printCourseList();
  47. printf("\nEnter the student's %d course number(s):\n\t", new.nCourses);
  48. for (int c = 0; c < new.nCourses; c++) {
  49. scanf("%d", &(new.courses[c].crn));
  50. if (!crnCorrect(new.courses[c].crn)) {
  51. printf("Invalid crn(s)!");
  52. return;
  53. }
  54. }
  55. end:
  56. for (int c = 0; c < MAXSTUDENTS; c++) {
  57. if (students[c].id == 0) students[c] = new;
  58. printf("\nStudent added successfully!\n");
  59. return;
  60. }
  61. printf("\nSorry - maximum number of students (%d) reached. \n", MAXSTUDENTS);
  62. printf(" Student not added\n");
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement