Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #define SIZE 4 // Define Number of Employees "SIZE" to be 2
- struct employee// Declare Struct Employee
- {
- int num;
- int age;
- double salary;
- };
- /* main program */
- int main(void) {
- int option = 0; //inititialize ints
- int NumOfEmp = 0;//inititialize ints
- int i = 0;//inititialize ints
- int idToUpdate = 0;
- int end = 0;
- int end2 = 0;
- printf("---=== EMPLOYEE DATA ===---\n\n"); //print title to screen
- struct employee emp[SIZE];// Declare a struct Employee array "emp" with SIZE elements and initialize all elements to zero
- 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
- printf("Exiting Employee Data Program. Good Bye!!!\n"); //printing exit message
- break;
- case 1: // Display Employee Data
- // @IN-LAB
- printf("EMP ID EMP AGE EMP SALARY\n"); // printing title
- printf("====== ======= ==========\n");
- for (i = 0; i < SIZE; i++) //iterate for all spots in array
- {
- if (emp[i].num > 0) { // checking if data is valid
- 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
- }
- }
- printf("\n");
- break;
- case 2: // Adding Employee
- // @IN-LAB
- printf("Adding Employee\n"); //print title
- printf("===============\n");
- if (NumOfEmp < (SIZE)) { // Check for limits on the array
- printf("Enter Employee ID: "); // add data accordingly
- scanf("%d", &emp[NumOfEmp].num);
- printf("Enter Employee Age: "); // add data accordingly
- scanf("%d", &emp[NumOfEmp].age);
- printf("Enter Employee Salary: "); // add data accordingly
- scanf("%lf", &emp[NumOfEmp].salary);
- printf("\n");
- NumOfEmp++;
- }
- else {
- printf("ERROR!!! Maximum Number of Employees Reached\n\n"); //print error message
- break;
- case 3:
- printf("\nUpdate Employee Salary\n");
- printf("===============\n");
- do {
- printf("Enter Employee ID: ");
- scanf("%d", &idToUpdate);
- //find userIDs index
- for (i = 0; i < SIZE; i++) {// for all spots in array
- if (idToUpdate == emp[i].num) {// if array contains entered number
- printf("The current salary is %.2lf", emp[i].salary);// print to screen
- printf("\nEnter Employee New Salary: ");// print to screen
- scanf("%lf", &emp[i].salary);
- printf("\n");//print to screen
- end++; // signal to end case
- }
- }
- } while (end == 0);
- break;
- case 4:
- printf("\nRemove Employee\n");
- printf("===============\n");
- do {
- printf("Enter Employee ID: ");
- scanf("%d", &idToUpdate);
- //find userIDs index
- for (i = 0; i < SIZE; i++) { // for all spots in array
- if (idToUpdate == emp[i].num) { // if array contains entered number
- printf("Employee %d will be removed\n", emp[i].num);// print to screen
- emp[i].num = 0; //reset to 0 num
- NumOfEmp--; //decrease number of employess so that another can be added
- end2++; // signal to end case
- printf("\n");// add line
- }
- }
- } while (end2 == 0);
- break;
- default:
- printf("ERROR: Incorrect Option: Try Again\n\n"); //print error message
- }
- }
- } while (option != 0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement