fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int unique_id() {
  5. static int last_id = 0;
  6. return ++last_id;
  7. }
  8.  
  9.  
  10.  
  11.  
  12.  
  13. void registerStudent(int n, char (name)[n][100], char (dob)[n][11], char (address)[n][100], char (contact_no)[n][11], int student_id[n]) {
  14. for (int i = 0; i < n; i++) {
  15. printf("Enter the details of student %d:\n", i + 1);
  16.  
  17. printf("Enter the name:\n");
  18. scanf("%99s", name[i]);
  19. while (getchar() != '\n'); // Clear the input buffer
  20.  
  21. printf("Enter the dob (YYYY/MM/DD)(including slash):\n");
  22. scanf("%10s", dob[i]);
  23. while (getchar() != '\n'); // Clear the input buffer
  24.  
  25. printf("Enter the address:\n");
  26. scanf("%99s", address[i]);
  27. while (getchar() != '\n'); // Clear the input buffer
  28.  
  29. printf("Enter the contact no:\n");
  30. scanf("%10s", contact_no[i]);
  31. while (getchar() != '\n'); // Clear the input buffer
  32.  
  33. student_id[i] = unique_id();
  34.  
  35. }
  36.  
  37. }
  38. void student_display(int n, char (name)[n][100], char (dob)[n][11], char (address)[n][100], char (contact_no)[n][11], int student_id[n])
  39. {
  40. for (int i = 0; i < n; i++) {
  41. printf("%99s \n", name[i]);
  42. printf("%10s \n", dob[i]);
  43. printf("%99s \n", address[i]);
  44. printf("%10s \n", contact_no[i]);
  45. printf("%d \n",student_id[i]);
  46. }
  47.  
  48. }
  49. void student_update(int search,int n, char (name)[n][100], char (dob)[n][11], char (address)[n][100], char (contact_no)[n][11], int student_id[n])
  50. {
  51. int count=-1;
  52. for(int i=0;i<n;i++)
  53. {
  54. if(search==student_id[i])
  55. count=i;
  56. }
  57. if(count>-1)
  58. {
  59. printf("the student record you wanted to update are found \n");
  60. printf("Enter the details of student :\n");
  61.  
  62. printf("Enter the name:\n");
  63. scanf("%99s", name[count]);
  64. while (getchar() != '\n');
  65.  
  66. printf("Enter the dob (YYYY/MM/DD)(including slash):\n");
  67. scanf("%10s", dob[count]);
  68. while (getchar() != '\n');
  69.  
  70. printf("Enter the address:\n");
  71. scanf("%99s", address[count]);
  72. while (getchar() != '\n');
  73.  
  74. printf("Enter the contact no:\n");
  75. scanf("%10s", contact_no[count]);
  76. while (getchar() != '\n');
  77. }
  78. else
  79. printf("the student id does not exist");
  80.  
  81. }
  82. void student_search(char search[100], int n, char name[n][100], char dob[n][11], char address[n][100], char contact_no[n][11], int student_id[n])
  83. {
  84. int count[100] = { -1 };
  85. int i;
  86.  
  87. for (i = 0; i < n; ++i)
  88. {
  89. if (strcmp(search, name[i]) == 0)
  90. {
  91. count[i] = 1;
  92. }
  93. }
  94. for(i=0;i<n;i++)
  95. {
  96. printf("the student details are given by \n");
  97. if(count[i]==1)
  98. {
  99. printf("%99s \n", name[i]);
  100. printf("%10s \n", dob[i]);
  101. printf("%99s \n", address[i]);
  102. printf("%10s \n", contact_no[i]);
  103. printf("%d \n",student_id[i]);
  104. }
  105. }
  106. }
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114. int main() {
  115. int max_students, i,id;
  116. printf("Enter the total number of students registered this year:");
  117. scanf("%d", &max_students);
  118.  
  119. // Arrays to store student details
  120. char name[max_students][100];
  121. char dob[max_students][11];
  122. char address[max_students][100];
  123. char contact_no[max_students][11];
  124. int student_id[max_students];
  125. char search[100];
  126. char ch;
  127. printf("Enter 1 to register the students, 2 for updating a student's details, 3 for searching the number of students with the same name, 4 for displaying the database, and 5 for exiting: ");
  128. do {
  129.  
  130. if (scanf(" %c", &ch) != 1) {
  131. while (getchar() != '\n');
  132. continue;
  133. }
  134.  
  135. switch(ch) {
  136. case '1':
  137. registerStudent(max_students, name, dob, address, contact_no, student_id);
  138. break;
  139. case '2':
  140. printf("Enter the student id to be searched: ");
  141. scanf("%d", &id);
  142. while (getchar() != '\n');
  143. student_update(id,max_students, name, dob, address, contact_no, student_id);
  144. break;
  145. case '3':
  146. printf("Enter the name to be searched:\n");
  147. scanf("%99s", search);
  148. while (getchar() != '\n');
  149. student_search(search,max_students, name, dob, address, contact_no, student_id);
  150. break;
  151. case '4':
  152. student_display(max_students, name, dob, address, contact_no, student_id);
  153. break;
  154. default:
  155. if (ch != '\n' && ch != ' ') {
  156. printf("Incorrect option\n");
  157. }
  158. break;
  159. }
  160. if (ch != '5') {
  161. printf("Enter another option (or '5' to exit): ");
  162. }
  163. } while (ch != '5');
  164.  
  165.  
  166.  
  167.  
  168. return 0;
  169. }
Time limit exceeded #stdin #stdout 5s 5304KB
stdin
45
stdout
Standard output is empty