I have a program that has a struct
of the following form:
struct deudores{
int NRR;
float deuda;
char nombre[30], domicilio[30], sexo [1];
}deudor[5];
I can only receive 5 data records, in this case I am handling debtors, I record 5 data that are composed as follows:
void Agregar(){
if (cont<5){
cout<<":::Agregar Deudor:::"<<endl;
deudor[cont].NRR = cont + 1;
cout<<"Nombre: ";
cin>>deudor[cont].nombre;
cin.ignore();
cout<<"Domicilio: ";
cin>>deudor[cont].domicilio;
cin.ignore();
cout<<"Sexo(H/M): ";
cin>>deudor[cont].sexo;
cin.ignore();
cout<<"Deuda: ";
cin>>deudor[cont].deuda;
cin.ignore();
cont++;
cout<<"\nDeudor registrado correctamente\n"<<endl;
system("pause");
}
else{
cout<<"\nNo hay espacio disponible\n"<<endl;
system ("pause");
}
}
But my problem is at the moment of deleting, because if I have 5 registered data which have a NRR(ID)
from 1 to 5 and I delete the data number 1 and I make a cont--;
cont that is my record counter, the record number 1 remains with values 0 but record number 5 is deleted. My method to delete is as follows:
void Eliminar(){
if (cont!=0){
bool band=false;
int dato;
cout<<"Ingresa el NRR a Eliminar: ";
cin>>dato;
for (int i=0; i<cont; i++){
if(dato == deudor[i].NRR){
cout<<":::Datos a Eliminar:::"<<endl;
cout << "========================" << endl;
cout<<"NRR: "<<deudor[i].NRR<<endl;
cout<<"Nombre: "<<deudor[i].nombre<<endl;
cout<<"Domicilio: "<<deudor[i].domicilio<<endl;
cout<<"Sexo: "<<deudor[i].sexo<<endl;
cout<<"Deuda: "<<deudor[i].deuda<<endl;
cout << "========================" << endl;
cout << "\nDeudor eliminado correctamente\n" << endl;
system ("pause");
band=true;
deudor[i].NRR = 0;
deudor[i].nombre[0]='\0';
*deudor[i].domicilio=0;
*deudor[i].sexo=0;
deudor[i].deuda=0.0;
cont--;
}
}
if(band==false){
cout<<"\nEl deudor ingresado no existe\n"<<endl;
system("pause");
}
}
else{
cout<<"\nNo hay deudores registrados\n"<<endl;
system ("pause");
}
}
I hope you understand me in the question.
Problem.
You have a static array of size 5
deudores
. Since the size is static, you cannot remove and insert records freely, but you must limit yourself to reusing the positions of the array. This can be problematic if you get random and/or non-consecutive positions removed from the array.Solution 1: Use stl containers .
Forget the fix of 5
deudores
and use a data holder appropriate to the task you want to perform. In your case you require inserts and deletes at random positions in your record collection, so the choice isstd::list
:The code could be like:
I've tried to preserve the code as much as possible but using STL functions and containers you don't need to limit the records to 5. Inserts are always done at the end (
push_back
) and deletion can be done at any position without problems even if you delete more than one record.Solution 2: Keep a record of free positions.
If you don't want to forget about the array, you will need to know in which positions data can or cannot be written, to do this use an array (of the same size) of free positions, the code could be as follows:
In this version the
deudor
is never deleted but rather it is marked as " available position ", keeping track of which positions are available is vital to keeping records well because there is the possibility of alternate records being deleted.style problems.
You have a number of problems in your code that you should be aware of:
deudores
is only one debtor while theredeudor
are several, interchange the names.char deudor::nombre[30]
) but it can also lead to errors at runtime if the name is longer than . long than the length of the array. The same for the home. Usestd::string
.5
throughout the code.If your idea is that when you add an element it is always entered at the end of the list, when you delete a record you have to move the following so that the gap is in the last position:
All the best.