Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- // Define Number of Employees "SIZE" to be 2
- #define SIZE 4
- // Declare Struct Employee
- struct employee {
- int id;
- int age;
- double salary;
- };
- /* main program */
- int main(void) {
- int i, j, k, option = 0, empFound = 0;
- int empCount = 0, empId;
- double new;
- printf("---=== EMPLOYEE DATA ===---\n\n");
- // Declare a struct Employee array "emp" with SIZE elements
- // and initialize all elements to zero
- struct employee emp[SIZE];
- do {
- // Print the option list
- printf("1. Display Employee Information\n");
- printf("2. Add Employee\n");
- printf("3. Update Employee Salary\n");
- printf("4. Remove Employee\n");
- printf("0. Exit\n\n");
- printf("Please select from the above options: ");
- // Capture input to option variable
- scanf("%d", &option);
- printf("\n");
- switch (option) {
- case 0: // Exit the program
- break;
- case 1: // Display Employee Data
- // @IN-LAB
- printf("EMP ID EMP AGE EMP SALARY\n");
- printf("====== ======= ==========\n");
- for (i = 0; i < empCount; i++) {
- if (emp[i].id > 0)
- printf("%6d%9d%11.2lf\n", emp[i].id, emp[i].age, emp[i].salary);
- }
- printf("\n");
- // Use "%6d%9d%11.2lf" formatting in a
- // printf statement to display
- // employee id, age and salary of
- // all employees using a loop construct
- // The loop construct will be run for SIZE times
- // and will only display Employee data
- // where the EmployeeID is > 0
- break;
- case 2: // Adding Employee
- // @IN-LAB
- printf("Adding Employee\n");
- printf("===============\n");
- // Check for limits on the array and add employee
- // data accordingly.
- if (empCount < 4) {
- printf("Enter Employee ID: ");
- scanf("%d", &emp[empCount].id);
- printf("Enter Employee Age: ");
- scanf("%d", &emp[empCount].age);
- printf("Enter Employee Salary: ");
- scanf("%lf", &emp[empCount].salary);
- printf("\n");
- if (emp[empCount].id > 0) {
- empCount++;
- }
- }
- else
- printf("ERROR!!! Maximum Number of Employees Reached\n\n");
- break;
- case 3:
- printf("Update Employee Information\n");
- printf("======================\n");
- do {
- printf("Enter Employee ID: ");
- scanf("%d", &empId);
- for (j = 0; j < empCount; j++) {
- if (emp[j].id == empId) {
- printf("The current salary is %.2lf\n", emp[j].salary);
- printf("Enter Employee New Salary: ");
- scanf("%lf", &new);
- printf("\n");
- emp[j].salary = new;
- empFound = 1;
- j = empCount;
- }
- }
- if (empFound == 0) {
- printf("*** ERROR: Employee ID not found! ***\n");
- }
- } while (empFound != 1);
- break;
- case 4:
- printf("Remove Employee\n");
- printf("===============\n");
- do {
- printf("Enter Employee ID: ");
- scanf("%d", &empId);
- for (k = 0; k < empCount; k++) {
- if (emp[k].id == empId) {
- printf("Employee %d will be removed\n\n", empId);
- emp[k].id = 0;
- k = empCount;
- empFound = 1;
- }
- }
- if (empFound == 0) {
- printf("*** ERROR: Employee ID not found! ***\n");
- }
- } while (empFound != 1);
- break;
- default:
- printf("ERROR: Incorrect Option: Try Again\n\n");
- }
- } while (option != 0);
- printf("Exiting Employee Data Program. Good Bye!!!\n\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement