SHARE
TWEET

Untitled

a guest Jun 17th, 2019 93 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define _CRT_SECURE_NO_WARNINGS
  2.  
  3. #include <stdio.h>
  4.  
  5. // Define Number of Employees "SIZE" to be 2
  6.  
  7. #define SIZE 4
  8.  
  9. // Declare Struct Employee
  10. struct employee {
  11.     int id;
  12.     int age;
  13.     double salary;
  14. };
  15.  
  16. /* main program */
  17. int main(void) {
  18.  
  19.     int i, j, k, option = 0, empFound = 0;
  20.     int empCount = 0, empId;
  21.     double new;
  22.  
  23.     printf("---=== EMPLOYEE DATA ===---\n\n");
  24.  
  25.     // Declare a struct Employee array "emp" with SIZE elements
  26.     // and initialize all elements to zero
  27.     struct employee emp[SIZE];
  28.     do {
  29.         // Print the option list
  30.         printf("1. Display Employee Information\n");
  31.         printf("2. Add Employee\n");
  32.         printf("3. Update Employee Salary\n");
  33.         printf("4. Remove Employee\n");
  34.         printf("0. Exit\n\n");
  35.         printf("Please select from the above options: ");
  36.         // Capture input to option variable
  37.         scanf("%d", &option);
  38.         printf("\n");
  39.         switch (option) {
  40.  
  41.         case 0: // Exit the program
  42.             break;
  43.  
  44.         case 1: // Display Employee Data
  45.                 // @IN-LAB
  46.             printf("EMP ID  EMP AGE EMP SALARY\n");
  47.             printf("======  ======= ==========\n");
  48.             for (i = 0; i < empCount; i++) {
  49.                 if (emp[i].id > 0)
  50.                     printf("%6d%9d%11.2lf\n", emp[i].id, emp[i].age, emp[i].salary);
  51.             }
  52.             printf("\n");
  53.             // Use "%6d%9d%11.2lf" formatting in a  
  54.             // printf statement to display
  55.             // employee id, age and salary of
  56.             // all  employees using a loop construct
  57.  
  58.             // The loop construct will be run for SIZE times
  59.             // and will only display Employee data
  60.             // where the EmployeeID is > 0
  61.             break;
  62.         case 2: // Adding Employee
  63.                 // @IN-LAB
  64.             printf("Adding Employee\n");
  65.             printf("===============\n");
  66.             // Check for limits on the array and add employee
  67.             // data accordingly.
  68.             if (empCount < 4) {
  69.                 printf("Enter Employee ID: ");
  70.                 scanf("%d", &emp[empCount].id);
  71.                 printf("Enter Employee Age: ");
  72.                 scanf("%d", &emp[empCount].age);
  73.                 printf("Enter Employee Salary: ");
  74.                 scanf("%lf", &emp[empCount].salary);
  75.                 printf("\n");
  76.                 if (emp[empCount].id > 0) {
  77.                     empCount++;
  78.                 }
  79.             }
  80.             else
  81.                 printf("ERROR!!! Maximum Number of Employees Reached\n\n");
  82.             break;
  83.         case 3:
  84.             printf("Update Employee Information\n");
  85.             printf("======================\n");
  86.             do {
  87.                 printf("Enter Employee ID: ");
  88.                 scanf("%d", &empId);
  89.                 for (j = 0; j < empCount; j++) {
  90.                     if (emp[j].id == empId) {
  91.                         printf("The current salary is %.2lf\n", emp[j].salary);
  92.                         printf("Enter Employee New Salary: ");
  93.                         scanf("%lf", &new);
  94.                         printf("\n");
  95.                         emp[j].salary = new;
  96.                         empFound = 1;
  97.                         j = empCount;
  98.                     }
  99.                 }
  100.                 if (empFound == 0) {
  101.                     printf("*** ERROR: Employee ID not found! ***\n");
  102.                 }
  103.             } while (empFound != 1);
  104.             break;
  105.         case 4:
  106.             printf("Remove Employee\n");
  107.             printf("===============\n");
  108.             do {
  109.                 printf("Enter Employee ID: ");
  110.                 scanf("%d", &empId);
  111.                 for (k = 0; k < empCount; k++) {
  112.                     if (emp[k].id == empId) {
  113.                         printf("Employee %d will be removed\n\n", empId);
  114.                         emp[k].id = 0;
  115.                         k = empCount;
  116.                         empFound = 1;
  117.                     }
  118.                 }
  119.                 if (empFound == 0) {
  120.                     printf("*** ERROR: Employee ID not found! ***\n");
  121.                 }
  122.             } while (empFound != 1);
  123.             break;
  124.         default:
  125.             printf("ERROR: Incorrect Option: Try Again\n\n");
  126.         }
  127.     } while (option != 0);
  128.     printf("Exiting Employee Data Program. Good Bye!!!\n\n");
  129.     return 0;
  130. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top