Advertisement
Guest User

Untitled

a guest
Oct 18th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2.  
  3. #include <stdio.h>
  4.  
  5.  
  6. #define SIZE 4 // Define Number of Employees "SIZE" to be 2
  7.  
  8.  
  9.  
  10. struct employee// Declare Struct Employee
  11. {
  12. int num;
  13. int age;
  14. double salary;
  15. };
  16.  
  17.  
  18. /* main program */
  19. int main(void) {
  20.  
  21. int option = 0; //inititialize ints
  22. int NumOfEmp = 0;//inititialize ints
  23. int i = 0;//inititialize ints
  24. int idToUpdate = 0;
  25. int end = 0;
  26. int end2 = 0;
  27. printf("---=== EMPLOYEE DATA ===---\n\n"); //print title to screen
  28.  
  29. struct employee emp[SIZE];// Declare a struct Employee array "emp" with SIZE elements and initialize all elements to zero
  30.  
  31. do {
  32. // Print the option list
  33. printf("1. Display Employee Information\n");
  34. printf("2. Add Employee\n");
  35. printf("3. Update Employee Salary\n");
  36. printf("4. Remove Employee\n");
  37. printf("0. Exit\n\n");
  38. printf("Please select from the above options: ");
  39.  
  40.  
  41. // Capture input to option variable
  42. scanf("%d", &option);
  43. printf("\n");
  44.  
  45. switch (option) {
  46. case 0: // Exit the program
  47. printf("Exiting Employee Data Program. Good Bye!!!\n"); //printing exit message
  48. break;
  49. case 1: // Display Employee Data
  50. // @IN-LAB
  51.  
  52. printf("EMP ID EMP AGE EMP SALARY\n"); // printing title
  53. printf("====== ======= ==========\n");
  54.  
  55. for (i = 0; i < SIZE; i++) //iterate for all spots in array
  56. {
  57. if (emp[i].num > 0) { // checking if data is valid
  58. printf("%6d%9d%11.2lf\n", emp[i].num, emp[i].age, emp[i].salary); // Use "%6d%9d%11.2lf" formatting in a printf statement to display employee id, age and salary
  59. }
  60.  
  61. }
  62. printf("\n");
  63.  
  64.  
  65. break;
  66. case 2: // Adding Employee
  67. // @IN-LAB
  68.  
  69. printf("Adding Employee\n"); //print title
  70. printf("===============\n");
  71.  
  72. if (NumOfEmp < (SIZE)) { // Check for limits on the array
  73. printf("Enter Employee ID: "); // add data accordingly
  74. scanf("%d", &emp[NumOfEmp].num);
  75.  
  76. printf("Enter Employee Age: "); // add data accordingly
  77. scanf("%d", &emp[NumOfEmp].age);
  78.  
  79. printf("Enter Employee Salary: "); // add data accordingly
  80. scanf("%lf", &emp[NumOfEmp].salary);
  81.  
  82. printf("\n");
  83. NumOfEmp++;
  84. }
  85. else {
  86. printf("ERROR!!! Maximum Number of Employees Reached\n\n"); //print error message
  87. break;
  88. case 3:
  89.  
  90. printf("\nUpdate Employee Salary\n");
  91. printf("===============\n");
  92. do {
  93. printf("Enter Employee ID: ");
  94. scanf("%d", &idToUpdate);
  95.  
  96. //find userIDs index
  97.  
  98. for (i = 0; i < SIZE; i++) {// for all spots in array
  99. if (idToUpdate == emp[i].num) {// if array contains entered number
  100. printf("The current salary is %.2lf", emp[i].salary);// print to screen
  101. printf("\nEnter Employee New Salary: ");// print to screen
  102. scanf("%lf", &emp[i].salary);
  103. printf("\n");//print to screen
  104. end++; // signal to end case
  105. }
  106. }
  107. } while (end == 0);
  108.  
  109. break;
  110.  
  111. case 4:
  112.  
  113. printf("\nRemove Employee\n");
  114. printf("===============\n");
  115. do {
  116. printf("Enter Employee ID: ");
  117. scanf("%d", &idToUpdate);
  118.  
  119. //find userIDs index
  120.  
  121. for (i = 0; i < SIZE; i++) { // for all spots in array
  122. if (idToUpdate == emp[i].num) { // if array contains entered number
  123. printf("Employee %d will be removed\n", emp[i].num);// print to screen
  124. emp[i].num = 0; //reset to 0 num
  125. NumOfEmp--; //decrease number of employess so that another can be added
  126. end2++; // signal to end case
  127. printf("\n");// add line
  128. }
  129. }
  130. } while (end2 == 0);
  131.  
  132. break;
  133.  
  134.  
  135.  
  136. default:
  137. printf("ERROR: Incorrect Option: Try Again\n\n"); //print error message
  138. }
  139.  
  140. }
  141. } while (option != 0);
  142.  
  143.  
  144. return 0;
  145.  
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement