I have a problem with the delete method in Scatter my delete code is the following:
void Empleados::Eliminar(){
int i,band=0,opcion,posanterior;
cout << ":::Eliminar Empleado:::" << endl;
cout << "Ingresa el nombre del empleado: ";
cin.getline(llave,35);
fstream a("Chambamex.txt",ios::in|ios::out);
if(!a.good()){
cout << "Archivo Chambamex no encontrado" << endl;
system("pause");
}else{
Dispersion();
cout<<"BASE: "<<d_base<<endl;
d_base=d_base*((sizeof(x)*4)+sizeof(int));
a.seekg(d_base,ios::beg);
a.read((char*)&cont,sizeof(int));
for(i=0;i<cont;i++){
a.read((char*)&x,sizeof(x));
if(strcmpi(llave,nombre)==0){
band=1;
cout << "========================" << endl;
cout << "Informacion del empleado" << endl;
cout << "========================" << endl;
cout << "Nombre: " << nombre << endl;
cout << "Domicilio: " << domicilio << endl;
cout << "Ciudad: " << ciudad << endl;
cout << "Telefono: " << telefono << endl;
cout << "Correo electronico: " << correo << endl;
cout << "========================" << endl;
cout << "Deseas eliminar este empleado" << endl;
cout << "========================" << endl;
cout << "[1]Si" << endl;
cout << "[0]No" << endl;
cout << "========================" << endl;
cout << "Ingresa tu opcion: ";
cin>>opcion;
switch(opcion){
case 0:
break;
case 1:
a.seekp(d_base,ios::beg);
a.write((char*)&x,sizeof(x));
cout << "Empleado eliminado correctamente" << endl;
system("pause");
break;
default:
cout<<"El numero de opcion escrito no es valido"<<endl;
system("pause");
}
}
}
if(band==0)
cout<<"No se encontro el registro, intente de nuevo";
}
a.close();
}
The problem is that if I have a registered data I enter it and when I delete it, when it is displayed it makes an infinite loop and everything is displayed empty but it is displayed indefinitely. I don't know if the problem is in the show or delete. My scatter method is as follows:
void Empleados::Dispersion(){
int j=0;
d_base=0;
while(j<strlen(llave)){
d_base=d_base+(100*llave[j])+(llave[j+1]%84645);
j=j+2;
}
d_base=d_base%100;
}
And my show method is as follows:
void Empleados::Mostrar(){
int i;
pos=0;
ifstream a("Chambamex.txt");
if(!a.good()){
cout<<"No existe el archivo";
}
else{
cout << ":::Mostrando Empleados:::" << endl;
cout << "========================" << endl;
while(!a.eof()){
a.seekg(pos,ios::beg);
a.read((char*)&cont,sizeof(int));
if(cont==0){
pos=pos+(4*sizeof(x))+sizeof(int);
}else{
for(i=0;i<cont;i++){
a.read((char*)&x,sizeof(x));
cout << "Nombre: " << nombre << endl;
cout << "Domicilio: " << domicilio << endl;
cout << "Ciudad: " << ciudad << endl;
cout << "Telefono: " << telefono << endl;
cout << "Correo electronico: " << correo << endl;
cout << "========================" << endl;
}
pos=pos+(4*sizeof(x))+sizeof(int);
}
}
system("pause");
}
a.close();
}
I want to know if I am doing the delete method in the correct way or that they can guide me in what way the data can be deleted with the Dispersion Method (Hashing Table).
first of all, Hassem right? It seems to me that the problem is that you sample based on what the counter tells you, that is, you read the counter and if it has a value of 3 you know that you must read 3 objects. Show() is fine, however once you try to remove
Here what you are doing is going back to the position that the scatter method told you and you are overwriting from where the counter is stored, which I imagine is what causes the error you describe. Now what you're supposed to do based on your code is decrement the counter
Once this is done, the difficult part is missing since you will notice that if you were looking for an element that was in the second of your four positions and you have all the positions occupied, the counter that was in 4 would remain in 3 and when showing it would only show you the first three for which the fourth element would be inaccessible and the second would continue to be displayed since it was not eliminated, here what you should do is that once you find the element to eliminate, check if there are more elements after that and go through them, I mean So if you delete the second element and the third and fourth elements are already captured, you loop through them, so that the third overwrites the second and the fourth overwrites the third, and then you decrement the counter. As we are not supposed to give the whole answer I do not put the code,