My program needs to create 4 employees id 222333444555 and


My program needs to create 4 employees (ID 222,333,444,555) and then delete one (ID 333) and add a new employee (ID 666). This all works fine, my issue is that once I display the array instead of putting the new employee (666) in place of the deleted employee (333), it just overwrites the last employee in the array (555) and I can't understand why. I also need to do this without reordering the items in the array, which is also causing an issue. I understand that writing employee[totalEmps].ID = 0; doesn't "delete" that employee and just hides it, but I am unsure of a better way. Output showing my issue is below as well as my code. Code in question is bolded.

1. Display Employee Information

2. Add Employee

3. Update Employee Salary

4. Remove Employee

0. Exit

Please select from the above options: 1

EMP ID EMP AGE EMP SALARY

====== ======= ==========

  222      22  22222.22

  333      33  33333.33

  444      44  44444.44

  555      55  55555.55

1. Display Employee Information

2. Add Employee

3. Update Employee Salary

4. Remove Employee

0. Exit

Please select from the above options: 4

Remove Employee

===============

Enter Employee ID: 333

Employee 333 will be removed

1. Display Employee Information

2. Add Employee

3. Update Employee Salary

4. Remove Employee

0. Exit

Please select from the above options: 2

 

Adding Employee

===============

Enter Employee ID: 666

Enter Employee Age: 66

Enter Employee Salary: 66666.66

1. Display Employee Information

2. Add Employee

3. Update Employee Salary

4. Remove Employee

0. Exit

Please select from the above options: 1

EMP ID EMP AGE EMP SALARY

====== ======= ==========

  222      22  22222.22

  444      44  44444.44

  666      66  66666.66

1. Display Employee Information

2. Add Employee

3. Update Employee Salary

4. Remove Employee

0. Exit

Please select from the above options:

#define _CRT_SECURE_NO_WARNINGS

#define SIZE 4

#include

struct Employee

{

   int ID; // Employee Identification.

   int age; // Employee Age.

   double salary; // Employee Salary.

};

int main(void) {

   int option = 0; // Variable for which option user picks in option list.

   int totalEmps = 0; // Variable for adding employees and displaying information.

   int Num = 0; // Variable for storing number of employees.

   int UserID = 0;

   int i = 0;

   struct Employee employee [SIZE] = { {0} }; // Declare a struct employee array "employee" with SIZE elements and initialize all elements to zero.

   printf("---=== EMPLOYEE DATA ===---nn");

   do { // Do while loop to continue printing out option list while user hasn't pressed 0 to exit.

      printf("1. Display Employee Informationn");

      printf("2. Add Employeen");

      printf("3. Update Employee Salaryn");

      printf("4. Remove Employeen");

      printf("0. Exitnn");

      printf("Please select from the above options: ");

      scanf("%d",&option);

      printf("n");

      switch (option) {

      case 0:   // Exit the program

         printf("Exiting Employee Data Program. Good Bye!!!n");

         break;

      case 1: // Display Employee Data

         printf("EMP ID EMP AGE EMP SALARYn");

         printf("====== ======= ==========n");

         for (Num = 0; Num <= SIZE; Num++) // For loop to continue printing out array if number of employees are less than SIZE.

         {

            if (employee[Num].ID > 0) // If statement to print array if employee ID is greater than 0.

               printf("%6d%9d%11.2lfn", employee[Num].ID, employee[Num].age, employee[Num].salary);

         }

         printf("n");

         break;

      case 2:   // Adding Employee

         printf("Adding Employeen");

         printf("===============n");

         if (totalEmps < SIZE) // If number of employees are less than SIZE, print out info for user to input and add 1 to variable "employee".

         {

            printf("Enter Employee ID: ");

            scanf("%d", &employee[totalEmps].ID);

            printf("Enter Employee Age: ");

            scanf("%d", &employee[totalEmps].age);

            printf("Enter Employee Salary: ");

            scanf("%lf", &employee[totalEmps].salary);

            printf("n");

            totalEmps++;         

         }

         else // Else print out error message.

         {

            printf("ERROR!!! Maximum Number of Employees Reachednn");

         }      

         break;

      case 3: // Updates Employee Salary.

         printf("Update Employee Salaryn");

         printf("======================n");

         do

         {

            i = 0;

            printf("Enter Employee ID: ");

            scanf("%d", &UserID);

            for (totalEmps = 0; totalEmps < SIZE; totalEmps++) {

               if (UserID == employee[totalEmps].ID) {

                  printf("The current salary is %.2lfn", employee[totalEmps].salary);

                  printf("Enter Employee New Salary: ");

                  scanf("%lf", &employee[totalEmps].salary);

                  printf("n");

                  i = 1;

               }

            }

            if (i != 1) {

               printf("*** ERROR: Employee ID not found! ***n");

            }

         } while (i < 1);

         break;

      case 4: // Removes Employees.

         printf("Remove Employeen");

         printf("===============n");

         do

         {

            i = 0;

            printf("Enter Employee ID: ");

            scanf("%d", &UserID);

            for (totalEmps = 0; totalEmps < SIZE; totalEmps++) {

               if (UserID == employee[totalEmps].ID) {

                  printf("Employee %d will be removedn", employee[totalEmps].ID);

                  printf("n");

                  employee[totalEmps].ID = 0;

                  i = 1;

               }

            }

            totalEmps = totalEmps - 1;

            if (i != 1) {

               printf("*** ERROR: Employee ID not found! ***n");

            }

         } while (i == 0);

         break;

      default: // Print error message if user inputs anything thats not on option list.

         printf("ERROR: Incorrect Option: Try Againnn");

      }

   } while (option != 0);

   return 0;

}

Solution Preview :

Prepared by a verified Expert
Business Management: My program needs to create 4 employees id 222333444555 and
Reference No:- TGS02936561

Now Priced at $30 (50% Discount)

Recommended (99%)

Rated (4.3/5)