I have this struct in C and I made an array from it
//Structure Definition
struct Employee
{
int Id;
char Name[20];
float Salary;
char Mobile[12];
};
int main()
{
int emparr_size = 3;
struct Employee emparr[emparr_size];
AddAllEmployees(emparr, emparr_size);
DisplayAllEmployees(emparr, emparr_size);
EditEmployee(emparr, emparr_size);
DeleteEmployee(emparr, emparr_size);
}
void AddAllEmployees(struct Employee * emp_ptr, int size)
{
for(int i=0; i<size; i++)
{
printf("Enter Employee %i Id: ", i+1);
scanf("%i", &emp_ptr->Id);
printf("Enter Employee %i Name: ", i+1);
scanf("%s", emp_ptr->Name);
printf("Enter Employee %i Salary: ", i+1);
scanf("%f", &emp_ptr->Salary);
printf("Enter Employee %i Mobile: ", i+1);
scanf("%s", emp_ptr->Mobile);
emp_ptr++;
}
}
void DisplayAllEmployees(struct Employee * emp_ptr, int size)
{
for(int i=0; i<size; i++)
{
printf("Employee %i Id is %i \n",i+1, emp_ptr->Id);
printf("Employee %i Name is %s \n",i+1, emp_ptr->Name);
printf("Employee %i Salary is %f \n",i+1, emp_ptr->Salary);
printf("Employee %i Mobile is %s \n",i+1, emp_ptr->Mobile);
emp_ptr++;
}
}
then I want to edit and delete one of it like so
void EditEmployee(struct Employee * emp_ptr, int size)
{
int index;
printf("Enter Employee Index: ");
scanf("%i",&index);
if(index>0 && index<=size)
{
printf("new Employee %i Id ",index);
scanf("%i", &emp_ptr[index-1].Id);
printf("new Employee %i Name ",index);
scanf("%s", emp_ptr[index-1].Name);
printf("new Employee %i Salary ",index);
scanf("%f", &emp_ptr[index-1].Salary);
printf("new Employee %i Mobile ",index);
scanf("%s", emp_ptr[index-1].Mobile);
}
else
{
printf("Invalid Index \n");
}
}
void DeleteEmployee(struct Employee * emp_ptr, int size)
{
int index;
printf("Enter Employee Index: ");
scanf("%i",&index);
struct Employee temp={0,"",0,""};
if(index>0 && index<=size)
{
emp_ptr[index-1]=temp;
}
else
{
printf("Invalid Index \n");
}
}
everything works fine except the delete function. It just replaces the deleted employee member with empty values like above :\
I have tried this code that deletes an employee by shifting other items in the array to its position like so.
void DeleteEmployee(struct Employee * emp_ptr, int size)
{
int index;
printf("Enter Employee Index: ");
scanf("%i",&index);
struct Employee temp={0,"",0,""};
if(index>0 && index<=size)
{
for(int i=0; i<size-1; i++) // important i < size-1
{
emp_ptr[index-1].Id=emp_ptr[index+1+i].Id;
emp_ptr[i+index].Name=emp_ptr[index+1+i].Name;
emp_ptr[i+index].Salary=emp_ptr[index+1+i].Salary;
emp_ptr[i+index].Mobile=emp_ptr[index+1+i].Mobile;
emp_ptr++;
}
}
else
{
printf("Invalid Index \n");
}
}
this gives me an error in the compilation
error: assignment to expression with array type
that's because the name and phone member are arrays of char :| and I can't do assignment so tried this
for(int i=0; i<size-1; i++) // important i < size-1
{
emp_ptr[i+index].Id=emp_ptr[index+1+i].Id;
strcpy(emp_ptr[index+1+i].Name,emp_ptr[i+index].Name);
emp_ptr[i+index].Id=emp_ptr[index+1+i].Salary;
strcpy(emp_ptr[index+1+i].Mobile,emp_ptr[i+index].Mobile);
emp_ptr++;
}
and it doesn't work fine!!!
Should I make a loop for the name and one phone or there is another way to do this without loops?