Advertisement
RozeT

ProjectCars

Mar 29th, 2020
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.92 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. struct Students *head = NULL;
  6. struct Students *temp = NULL;
  7. int i =0;
  8.  
  9. struct Students{
  10.         char firstName[15];
  11.         char lastName[15];
  12.         char grade[10];
  13.         int ID;
  14.         double score;
  15.         struct Students *next;
  16. };
  17.  
  18. void text()
  19. {
  20.     printf("--------------------------\n");
  21.     printf("Enter a new student\n");
  22.     printf("Check student's info\n");
  23.     printf("Edit student's info\n");
  24.     printf("Exit\n");
  25.     printf("--------------------------\n");
  26. }
  27.  
  28. void new()
  29. {
  30.     if(i == 0){
  31.     i++;
  32.     head = malloc(sizeof(struct Students));
  33.     if(head == NULL) perror("NEW HEAD ERROR");
  34.     printf("Student's name : "); scanf("%s %s", head->firstName, head->lastName);
  35.     printf("Student's grade : "); scanf("%s", head->grade);
  36.     printf("Student's ID : "); scanf("%d", &(head->ID));
  37.     printf("Student's score : "); scanf("%lf", &(head->score));
  38.     head->next = NULL;
  39.  
  40.     }
  41.     else{
  42.     struct Students *student = malloc(sizeof(struct Students));
  43.     if(student == NULL) perror("NEW STUDENT ERROR");
  44.  
  45.     temp = head;
  46.     head = student;
  47.     head->next = temp;
  48.  
  49.  
  50.     printf("Student's name : "); scanf("%s %s", head->firstName, head->lastName);
  51.     printf("Student's grade : "); scanf("%s", head->grade);
  52.     printf("Student's ID : "); scanf("%d", &(head->ID));
  53.     printf("Student's score : "); scanf("%lf", &(head->score));
  54.     }
  55. }
  56.  
  57. void check(struct Students *current, int ID)
  58. {
  59.     while(current != NULL){
  60.         if(current->ID == ID){
  61.             printf("ID : %d\n", current->ID);
  62.             printf("Name : %s %s\n", current->firstName, current->lastName);
  63.             printf("Grade : %s\n", current->grade);
  64.             printf("Score : %.2lf\n", current->score);
  65.             break;
  66.         }
  67.         else
  68.             current = current->next;
  69.     }
  70.  
  71.  
  72.  
  73. }
  74. int main()
  75. {
  76.     int scanID = 0;
  77.     FILE *fp = fopen("student.dat", "w");
  78.     if(fp == NULL)
  79.         perror("ERROR");
  80.  
  81.     printf("Welcome! Choose an action.\n");
  82.     char *action = malloc(10*sizeof(char));
  83.     if (action == NULL) perror("MAIN ACTION ERROR");
  84.     text();
  85.  
  86.  
  87.     while(1){
  88.         scanf("%s", action);
  89.         if(strcmp(action, "New")== 0){
  90.                 printf("\n");
  91.             new();
  92.         }
  93.        else if(strcmp(action, "Check") == 0){
  94.             printf("\nEnter student's ID : ");
  95.             scanf("%d",&scanID);
  96.             check(head, scanID);
  97.         }
  98.         else
  99.         {
  100.             while(head != NULL){
  101.            fwrite(head, sizeof(struct Students), 1, fp);
  102.  
  103.         if(fwrite != 0)
  104.             printf("contents to file written successfully !\n");
  105.         else
  106.             printf("error writing file !\n");
  107.  
  108.  
  109.  
  110.             head = head->next;
  111.  
  112.             }
  113.             fclose(fp);
  114.             return 96;
  115.  
  116.         }
  117.  
  118.          printf("\nChoose another action : ");
  119.  
  120.     }
  121.  
  122.  
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement