problem where after removing employee and adding a new employee adds the employee at the end of the stack rather than deleted position.
Code:
_CRT_SECURE_NO_WARNINGS
#define SIZE 4
#include<stdio.h>
struct Employee
{
int id;
int age;
double salary;
};
int main(void)
{
int option;
int i =0, j =0;
int limit =0;
int idsearch =0;
int idsave =0;
int count =0;
structEmployee 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)
{
case0:
printf("Exiting Employee Data Program. Good Bye!!! ");
break;
case1:
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;
case2:
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;
case3:
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;
elseif(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;
case4:
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;
}
elseif(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 =0;
}
}
limit -=1;
}
}while(count);
break;
default:
printf("ERROR: Incorrect Option: Try Again\n\n");
}
}while(option !=0);
}
am trying to add the new employee information after deleting a particular employee, but adds the new employee info at the end of the stack, instead of adding it to deleted position. I'm stuck on how to do it, please help me out