Guest User

Untitled

a guest
Oct 23rd, 2026
6
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.38 KB | Source Code | 0 0
  1. //Program to add, modify and remove data about workers.
  2. //Do you approve it?
  3.  
  4. //P.S.: Compile with -lm option if you got a linker error.
  5.  
  6. #include <stdio.h>
  7. #include <ctype.h>
  8. #include <math.h>
  9.  
  10.  
  11. struct data{
  12.     char first_name[255];
  13.     char second_name[255];
  14.     char position[255];
  15.     char data_read[65535];
  16.  
  17.     int age;
  18.     int days_since_employement;
  19.     int days_until_next_vacation;
  20.     int salary;
  21.     int years_of_experience;
  22. };
  23.  
  24. void get_string(char[], int);
  25. int get_number();
  26. int save_data(struct data, char[]);
  27. int main(){
  28.  
  29.     char input[255];
  30.  
  31.     int i,c;
  32.     int exit = 0;
  33.  
  34.     struct data worker;
  35.  
  36.     while(!exit){
  37.         if(c != '1'){
  38.             printf("Workers assignement program.\nThis program is used to add or to remove data about workers.\n\n");
  39.             printf("\t\t-To add a new worker, insert 1\n\t\t-To remove a worker, insert 2\n\t\t-To modify data about a worker, insert 3\n");
  40.             printf("\t\t-To seek help, insert 4\n\t\t-To quit, insert 'q'\n\n");
  41.             printf("\\>");
  42.             c = getchar();
  43.         }
  44.         switch(c){
  45.             case '1':
  46.                 while(c == '1'){
  47.         //second getchar to flush stdin.
  48.                     getchar();
  49.                     printf("First name: ");
  50.                     get_string(worker.first_name, 255);
  51.                     printf("Second name: ");
  52.                     get_string(worker.second_name, 255);
  53.                     printf("Position: ");
  54.                     get_string(worker.position, 255);
  55.  
  56.                     printf("Age: ");
  57.                     worker.age = get_number();
  58.                     printf("Days since employement: ");
  59.                     worker.days_since_employement = get_number();
  60.                     printf("Days until next vacation: ");
  61.                     worker.days_until_next_vacation = get_number();
  62.                     printf("Salary(in USD): ");
  63.                     worker.salary = get_number();
  64.                     printf("Years of experience: ");
  65.                     worker.years_of_experience = get_number();
  66.                     printf("\nWorker's data:\n");
  67.  
  68.                     printf("\tFirst name: %s\n", worker.first_name);
  69.                     printf("\tSecond name: %s\n", worker.second_name);
  70.                     printf("\tAge: %d\n", worker.age);
  71.                     printf("\tDays since employement: %d\n", worker.days_since_employement);
  72.                     printf("\tDays until next vacation: %d\n", worker.days_until_next_vacation);
  73.                     printf("\tYears of experience: %d\n", worker.years_of_experience);
  74.  
  75.                     printf("Is this correct?(Y/n)");
  76.  
  77.                     while((c = getchar()) != 'Y' && c != 'y' && c != 'N' && c != 'n')
  78.                         printf("Is this correct?(Y/n)");
  79.                     if(c == 'Y' || c == 'y'){
  80.                         getchar();
  81.                         printf("Please re-insert the first name of the worker: ");
  82.                         get_string(input, 255);
  83.                         save_data(worker, input);
  84.                         printf("Information saved. Press enter to proceed\n\n");
  85.                         getchar();
  86.                     }
  87.                     else if(c == 'N' || c == 'n'){
  88.                         printf("Operation reset.\n");
  89.                     //this is supposed to make the case start over.
  90.                         c = '1';
  91.                     //again, flushing stdin. Would appreciate an explanation with a solution.
  92.                         getchar();
  93.                     }
  94.                 }
  95.                 break;
  96.             case '2':
  97.                 getchar();
  98.                 printf("Please insert the worker's name: ");
  99.                 get_string(input, 255);
  100.                 printf("\nDo you confirm the deletion of the data of this worker?(Y/n)");
  101.  
  102.                 while((c = getchar()) != 'Y' && c != 'y' && c != 'N' && c != 'n')
  103.                     printf("\nDo you confirm the deletion of the data of this worker?(Y/n)");
  104.  
  105.                 if(c == 'Y' || c == 'y'){
  106.                     if(remove(input))
  107.                         printf("Failed to remove the file\n");
  108.                     else
  109.                         printf("File removed successfully.\n\n");
  110.                     getchar();
  111.                 }
  112.                 else if(c == 'N' || c == 'n')
  113.                     printf("Operation aborted.\n");
  114.                 break;
  115.             case '3':
  116.                 FILE *fp;
  117.                 getchar();
  118.                 printf("Please insert the worker's name: ");
  119.                 get_string(input, 255);
  120.                 printf("\n\n\n");
  121.                 if((fp = fopen(input, "r+")) == NULL){
  122.                     printf("Error while reading data");
  123.                     return -1;
  124.                 }
  125.                 else
  126.                     while((c = getc(fp)) != EOF)
  127.                         printf("%c",c);
  128.                 printf("\nModify this file?(Y/n)");
  129.             //Pretty lazy, isn't?
  130.                 while((c = getchar()) != 'Y' && c != 'y' && c != 'N' && c != 'n')
  131.                     printf("\nModify this file?(Y/n)");
  132.  
  133.                 if(c == 'Y' || c == 'y')
  134.                     c = '1';
  135.  
  136.                 else if(c == 'N' || c == 'n'){
  137.                     printf("Operation aborted.\n");
  138.                     getchar();
  139.                 }
  140.                 break;
  141.             case '4':
  142.                 getchar();
  143.                 printf("This program is used to create, modify and delete info about the workers.\n");
  144.                 printf("When data is created, it will be saved in a file with a desired name, it is preferred that the name of the file matches the first name of the employee for easier access.\n\n\n");
  145.                 break;
  146.             case 'q':
  147.                 exit++;
  148.                 break;
  149.             default:
  150.                 printf("Unknown selection.\n");
  151.                 break;
  152.         }
  153.     }
  154.     return 0;
  155. }
  156.  
  157. void get_string(char string[], int max){
  158.     int c, i;
  159.     for(c = i = 0; (c = getchar()) != EOF && c != '\n' && i < max;)
  160.         string[i++] = c;
  161.     string[i] = 0;
  162.     if(i >= max)
  163.         printf("Notice from get_string(): max amount of characters reached.\n");
  164.     return;
  165. }
  166.  
  167. int get_number(){
  168.     int c, i, j, k;
  169.     char inputted_number_in_char[255];
  170.  
  171.     for(c = i = 0; (c = getchar()) != EOF && c != '\n' && i < 255;)
  172.         if(isdigit(c))
  173.             inputted_number_in_char[i++] = c;
  174.         else{
  175.             printf("Notice from get_number(): input contains a non-digit character\n.");
  176.             return 0;
  177.         }
  178.     inputted_number_in_char[i--] = 0;
  179.     j = k = 0;
  180.     while(i >= 0)
  181.         j += ((inputted_number_in_char[i--] - '0') * pow(10,k++));
  182.     return j;
  183. }
  184.  
  185. int save_data(struct data employee, char file_name[]){
  186.     FILE *fp;
  187.     char part0[] = "\t EMPLOYEE INFORMATION \n\n\0";
  188.     char part1[] = "\t\tFirst name: \0";
  189.     char part2[] = "\t\tSecond name: \0";
  190.     char part3[] = "\t\tPosition: \0";
  191.     char part4[] = "\t\tAge: \0";
  192.     char part5[] = "\t\tDays since employement: \0";
  193.     char part6[] = "\t\tDays until next vacation: \0";
  194.     char part7[] = "\t\tSalary: \0";
  195.     char part8[] = "\t\tYears of experience: \0";
  196.  
  197.     char input[255];
  198.  
  199.     if((fp = fopen(file_name, "w")) == NULL){
  200.         printf("Error: can't create file %s", file_name);
  201.         return -1;
  202.     }
  203.  
  204.     int i = 0;
  205.     while(part0[i] != '\0'){
  206.         putc(part0[i++], fp);
  207.     }
  208.     i = 0;
  209.     while(part1[i] != '\0'){
  210.         putc(part1[i++], fp);
  211.     }
  212.     i = 0;
  213.     while(employee.first_name[i] != '\0'){
  214.         putc(employee.first_name[i++], fp);
  215.     }
  216.     putc('\n', fp);
  217.     i = 0;
  218.     while(part2[i] != '\0'){
  219.         putc(part2[i++], fp);
  220.     }
  221.     i = 0;
  222.     while(employee.second_name[i] != '\0'){
  223.         putc(employee.second_name[i++], fp);
  224.     }
  225.     putc('\n', fp);
  226.     i = 0;
  227.     while(part3[i] != '\0'){
  228.         putc(part3[i++], fp);
  229.     }
  230.     i = 0;
  231.     while(employee.position[i] != '\0'){
  232.         putc(employee.position[i++], fp);
  233.     }
  234.     putc('\n', fp);
  235.     i = 0;
  236.     while(part4[i] != '\0'){
  237.         putc(part4[i++], fp);
  238.     }
  239.     //Thanks, Dave Jarvis and cnicutar from Stack Overflow
  240.     sprintf(input, "%d", employee.age);
  241.     i = 0;
  242.     while(input[i] != '\0'){
  243.         putc(input[i++], fp);
  244.     }
  245.     putc('\n', fp);
  246.     i = 0;
  247.     while(part5[i] != '\0'){
  248.         putc(part5[i++], fp);
  249.     }
  250.     sprintf(input, "%d", employee.days_since_employement);
  251.     i = 0;
  252.     while(input[i] != '\0'){
  253.         putc(input[i++], fp);
  254.     }
  255.     putc('\n', fp);
  256.     i = 0;
  257.     while(part6[i] != '\0'){
  258.         putc(part6[i++], fp);
  259.     }
  260.     sprintf(input, "%d", employee.days_until_next_vacation);
  261.     i = 0;
  262.     while(input[i] != '\0'){
  263.         putc(input[i++], fp);
  264.     }
  265.     putc('\n', fp);
  266.     i = 0;
  267.     while(part7[i] != '\0'){
  268.         putc(part7[i++], fp);
  269.     }
  270.     sprintf(input, "%d", employee.salary);
  271.     i = 0;
  272.     while(input[i] != '\0'){
  273.         putc(input[i++], fp);
  274.     }
  275.     putc('\n', fp);
  276.     i = 0;
  277.     while(part8[i] != '\0'){
  278.         putc(part8[i++], fp);
  279.     }
  280.     sprintf(input, "%d", employee.years_of_experience);
  281.     i = 0;
  282.     while(input[i] != '\0'){
  283.         putc(input[i++], fp);
  284.     }
  285.     putc('\n', fp);
  286.  
  287.     fclose(fp);
  288.     return 0;
  289. }
Advertisement
Add Comment
Please, Sign In to add comment