Advertisement
Guest User

Untitled

a guest
Mar 29th, 2020
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.77 KB | None | 0 0
  1. #define _GNU_SOURCE
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. int main()
  6. {
  7.     menu();
  8.     return 0;
  9. }
  10.  
  11. void menu(){
  12.     int choice = -1;
  13.     while (choice != 0){
  14.         printf("what do you want to do:\n");
  15.         printf("[0] exit\n");
  16.         printf("[1] add new worker\n");
  17.         printf("[2] list workers\n");
  18.         printf("[3] delete worker\n");
  19.         printf("[4] exit\n");
  20.         scanf("%d", &choice);
  21.         switch(choice){
  22.         case 1:
  23.             newWorker();
  24.             break;
  25.         case 2:
  26.             listWorkers();
  27.             break;
  28.         case 3:
  29.             printf("delete worker");
  30.             break;
  31.         case 0:
  32.             printf("exit");
  33.             break;
  34.         default:
  35.             printf("invalid input\n");
  36.         }
  37.     }
  38. }
  39.  
  40. void newWorker(){
  41.     int ndays;
  42.     char days[5][10];
  43.     char firstName[100];
  44.     char lastName[100];
  45.     char address[100];
  46.     printf("how many days will the worker work: ");
  47.     scanf("%d", &ndays);
  48.     if(0 < ndays && ndays < 6){
  49.         for(int i = 0; i < ndays; i++){
  50.             printf("Input the %d. day: ", i);
  51.             scanf("%s", &days[i]);
  52.         }
  53.     }else{
  54.         printf("worker can only work 1-5 days a week\n");
  55.         newWorker();
  56.     }
  57.     printf("what is the worker's first name: ");
  58.     scanf("%s", &firstName);
  59.     printf("what is the worker's last name:");
  60.     scanf("%s", &lastName);
  61.     printf("what is the worker's address(no space allowed):");
  62.     scanf("%s", &address);
  63.     char dataLine[50]; //a string to concate the days into
  64.     strcpy(dataLine, days[0]); //copying the first day into the string
  65.     strcat(dataLine, " ");
  66.     for(int i = 1; i < ndays; i++){
  67.         strcat(dataLine, days[i]);
  68.         strcat(dataLine, " ");
  69.     }
  70.     printf("%s\n", dataLine);
  71.     FILE *workerList = fopen("workerlist.txt", "a");
  72.     fprintf(workerList, "%s %s %s %d %s\n", firstName, lastName, address, ndays, dataLine);
  73.     fclose(workerList);
  74. }
  75.  
  76. void listWorkers(){
  77.     FILE * f;
  78.     f=fopen("workerlist.txt","r");
  79.     if (f==NULL){perror("File opening error\n"); exit(1);}
  80.     char line[350];
  81.     while (!feof(f)){
  82.         fgets(line,sizeof(line),f);
  83.         char lastName[100], firstName[100], address[100];
  84.         strcpy(lastName, strtok(line, " "));
  85.         strcpy(firstName, strtok(NULL, " "));
  86.         strcpy(address, strtok(NULL, " "));
  87.         printf("%s %s lives at %s\n    they are available on: ", lastName, firstName, address);
  88.         char cnDays[100];
  89.         strcpy(cnDays, strtok(NULL, " "));
  90.         int nDays = atoi(cnDays);
  91.         char days[5][10];
  92.         for (int i = 0; i < nDays; i++){
  93.             strcpy(days[i], strtok(NULL, " "));
  94.             printf("%s ", days[i]);
  95.         }
  96.     }
  97.     printf("\n");
  98.     fclose(f);
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement