I am applying the queue concept in a program, the program can log data, display it and delete it. This is the structure I use:
struct personal {
char nombre [40];
char apellido [40];
int edad;
float sueldo;
} ;
int j,i =0; //indices
int opc=0;
char k [3];
personal cliente [100];
My problem is at the time of deleting the data, I want to delete the data from the starting position (0) and then shift back the remaining data:
void elimina(){
do{
system("cls");
printf("\n EL CLIENTE A ELIMINAR ES : \n ");
printf("\n nombre : %s \n ",cliente[0].nombre );
printf("\n apellido: %s \n ",cliente[0].apellido );
printf("\n edad : %d \n ",cliente[0].edad );
printf("\n sueldo : %f \n ",cliente[0].sueldo );
for ( j = 0; j < i; j++)
{
cliente[j].nombre= cliente[j+1].nombre; //Intento fallido
cliente[j].apellido= cliente[j+1].apellido; //Intento fallido
cliente[j].edad = cliente[j+1].edad;
cliente[j].sueldo = cliente[j+1].sueldo ;
}
printf("\n Desea elimnar otro cliente: [si=1/no=2] \n");
scanf("%d" ,&opc);
i--;
getch();
}while (opc==1 && i>0);
}
The variable cliente[j].edad
and cliente[j].sueldo
are moved correctly, my problem is with string variables, I don't know how to move them. There I tried, but it does not work correctly.
There are much simpler ways to do it. Since you are using a
struct
, the size of your data is known and fixed. Therefore, we can perfectly use the functionmemcpy( )
(in the header<string.h>
). This function allows us to move an entire instance ofpersonal
, without having to copy member by member: