I am stuck with logic on how to place the new array value to deleted position
The below code can remove an employee and add new employee information at the bottom of the stack, however it has to add the employee to the deleted position.
Code:
#define _CRT_SECURE_NO_WARNINGS
#define SIZE 4
#include <stdio.h>
struct Employee
{
int id;
int age;
double salary;
};
int main(void)
{
int option,flag=0;
int i = 0, j = 0;
int limit = 0;
int idsearch = 0;
int idsave = 0;
int count = 0;
struct Employee emp[SIZE] = { 0,0,0.0 };
printf("---=== EMPLOYEE DATA ===---\n\n");
do {
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: ");
scanf("%d", &option);
printf("\n");
switch (option)
{
case 0:
printf("Exiting Employee Data Program. Good Bye!!! ");
break;
case 1:
printf("EMP ID EMP AGE EMP SALARY\n");
printf("====== ======= ==========\n");
for (i = 0;i < SIZE;i++)
{
if (emp[i].id > 0)
printf(" % 6d % 9d% 11.2lf", emp[i].id, emp[i].age, emp[i].salary);
printf("\n");
}
printf("\n");
break;
case 2:
printf("Adding Employee\n");
printf("===============\n");
if (limit < SIZE)
{
printf("Enter Employee ID: ");
scanf("%d", &emp[limit].id);
printf("Enter Employee Age : ");
scanf("%d", &emp[limit].age);
printf("Enter Employee Salary : ");
scanf("%lf", &emp[limit].salary);
limit++;
}
else
printf(" ERROR!!! Maximum Number of Employees Reached \n");
printf("\n");
break;
case 3:
printf("Update Employee Salary\n");
printf("======================\n");
do
{
count = 1;
printf("Enter Employee ID: ");
scanf("%d", &idsearch);
for (i = 0;i < limit;i++)
{
if (emp[i].id == idsearch)break;
else if (i == limit - 1)
{
printf("*** ERROR: Employee ID not found!***\n");
}
}
if (i != limit)
{
printf("The current salary is %.2f\n", emp[i].salary);
printf("Enter Employee New Salary: ");
scanf("%lf", &emp[i].salary);
printf("\n");
count = 0;
}
} while (count);
break;
case 4:
printf("Remove Employee\n");
printf("===============\n");
do
{
count = 1;
printf("Enter Employee ID: ");
scanf("%d", &idsearch);
for (i = 0;i < limit;i++)
{
if (emp[i].id==idsearch)
{
break;
}
else if (i == limit - 1)
{
printf("*** ERROR: Employee ID not found!***\n");
}
}
if (i != limit)
{
count = 0;
idsave = i;
printf("Employee %d will be removed\n\n", emp[idsave].id);
for (i = idsave;i < limit;i++)
{
if (i != limit - 1)
{
emp[i] = emp[i+1];
}
else
{
emp[i].id = -1;
}
}
limit -= 1;
}
} while (count);
break;
default:
printf("ERROR: Incorrect Option: Try Again\n\n");
}
} while (option != 0);
}