How do I delete values from an array in c ++?

1

my doubt is at the moment of eliminating values from an array and going through it does not erase, here is part of my code (I find the position to be eliminated but it does not do it)

#include<iostream>
#include<stdlib.h>
#include<stdio.h>

using namespace std;

int contador;
class Cliente //Clase
{
    char nombre[5][10]; //atributos de la clase
    char apellido[5][10];
     float estatura[5];
    int edad[5];
    char situacion_civil[5][10];


    //Metodos
    public:
        void alta();
        void consulta_uno();
        void consulta_general();
        void baja();
        void modificar();

};

 void Cliente::alta()
 {
    system("cls");
    cout<<"//////////////ALTA/////////////////////";
    string nombre_buscado;
    string apellido_buscado;
    if(contador>=5)
    {
        cout<<"La lista esta llena";
    }
    else
    {


cout<<"    \nIngrese el nombre y apellido del cliente:";cin >> nombre_buscado>>apellido_buscado;
int i;
for( i=0; i<contador; i++ )
{
  if( nombre[i] == nombre_buscado && apellido[i] == apellido_buscado )
    break;
}

if( i < contador )
{


    cout<<"El usuario ya esta registrado...";
  // Ya has encontrado el registro que buscas
  // ...


}
else
{ 
 //No esta en la lista
 cout<<"   El usuario:"<<nombre_buscado<<" "<<apellido_buscado<<" No esta en la lista, favor de capturar."<<endl;
 cout<<"   Ingrese el nombre y apellido:";cin>>nombre[i]>>apellido[i];
 cout<<"   Ingrese la estatura:";cin>>estatura[i];
cout<<"    Ingrese la situacion civil del cliente :";cin>>situacion_civil[i];
cout<<"    Ingrese la edad:          ";cin>>edad[i];cout<<endl;
cout<<"     El ID del cliente es: "<<contador+1<<endl;
 contador++;

system("pause");

}

}


}


void Cliente::consulta_uno()
{
    system("cls");
    cout<<"##############CONSULTA##############";
    string nombre_buscado;
    string apellido_buscado;
    cout<<"\nIngrese el nombre y apellido del cliente:";cin >> nombre_buscado>>apellido_buscado;

int i;
for( i=0; i<contador; i++ )
{
  if( nombre[i] == nombre_buscado && apellido[i] == apellido_buscado )
    break;
}

if( i < contador )
{
    system("cls");
cout<<"############USUARIO ENCONTRADO############"<<endl;
cout<<"SU ID ES:"<<i+1<<endl;
cout<<"SU NOMBRE Y APELLIDO ES:"<<nombre[i]<<" "<<apellido[i]<<endl;
cout<<"SU ESTATURA ES:"<<estatura[i]<<endl;
cout<<"Su estatus civil es:"<<situacion_civil[i]<<endl;
cout<<"La edad es: "<<edad[i];cout<<endl;
system("pause");    
  // Ya has encontrado el registro que buscas
  // ...


}
else
{
 //No esta en la lista
 cout<<"Este usuario no esta registrado...";
system("pause");

}

}

void Cliente::modificar()
{

    system("cls");
    cout<<"##############MODIFICAR##############";
    string nombre_buscado;
    string apellido_buscado;
    cout<<"\nIngrese el nombre y apellido del cliente:";cin >> nombre_buscado>>apellido_buscado;

int i;
for( i=0; i<contador; i++ )
{
  if( nombre[i] == nombre_buscado && apellido[i] == apellido_buscado )
    break;
}

if( i < contador )
{
    system("cls");
cout<<"############USUARIO ENCONTRADO############"<<endl;
cout<<"SU ID ES:"<<i+1<<endl;
cout<<"SU NOMBRE Y APELLIDO ES:"<<nombre[i]<<" "<<apellido[i]<<endl;
cout<<"SU ESTATURA ES:"<<estatura[i]<<endl;
cout<<"Su estatus civil es:"<<situacion_civil[i]<<endl;
cout<<"Su edad es:"<<edad[i]<<endl;
//MODIFICAR EL USUARIO
cout<<"###MODIFICAR###";
 cout<<"   Ingrese el nombre y apellido:";cin>>nombre[i]>>apellido[i];
 cout<<"   Ingrese la estatura:";cin>>estatura[i];
cout<<"    Ingrese la situacion civil del cliente :";cin>>situacion_civil[i];
cout<<"    Ingrese la edad:          ";cin>>edad[i];cout<<endl;

  // Ya has encontrado el registro que buscas
  // ...


}
else
{
 //No esta en la lista
 cout<<"Este usuario no esta registrado...";
system("pause");

}


}

 void Cliente::baja()
{
    system("cls");
    cout<<"#####BAJA#####";
    string nombre_buscado;
    string apellido_buscado;

    cout<<"\nIngrese el nombre y apellido del cliente:";cin >> nombre_buscado>>apellido_buscado;
int i,posicion;
char salir; //Para seleccionar en S/N



for( i=0; i<contador; i++ )
{
  if( nombre[i] == nombre_buscado && apellido[i] == apellido_buscado )
    break;
}

if( i < contador )
{



  // Ya has encontrado el registro que buscas
  // ...
  posicion=i;
  system("cls");
  cout<<"############USUARIO ENCONTRADO############"<<endl;
cout<<"SU ID ES:"<<i+1<<endl;
cout<<"SU NOMBRE Y APELLIDO ES:"<<nombre[i]<<" "<<apellido[i]<<endl;
cout<<"SU ESTATURA ES:"<<estatura[i]<<endl;
cout<<"Su estatus civil es:"<<situacion_civil[i]<<endl;
cout<<"Su edad es:"<<edad[i];cout<<endl;
cout<<"Desea realmente borrar este usuario? S/N:";cin>>salir;
if('s'==salir || 'S'==salir)
{

}
else
{
    cout<<"Cancelando...";
}


}
else
{
 //No esta en la lista


system("pause");

}

}





Cliente pt; //Declaro una variable de tipo Cliente para llamar los metodos.

int main()
{



}
    
asked by Pedro Robles 27.09.2017 в 06:30
source

1 answer

1

This class has several concept errors. I do not know if the statement itself forces you to commit those errors or if you are doing them by yourself.

The first thing is that it is not conceivable that a class called Cliente really acts as if it were a list of clients. The logical, normal and expected is that class Cliente is only responsible for managing the data of a client (for that is called Cliente and not ListaClientes ), leaving the management of the list to another class. Doing this simplifies the design and management enormously:

struct Cliente
{
  char nombre[10];          // es preferible std::string nombre
  char apellido[10];        // es preferible std::string apellido;
  float estatura;
  int edad;
  char situacion_civil[10]; // es preferible std::string situacion_civil
};

The management of the list can be implemented by hand or resort to the containers of the STL:

Cliente clientes[5]; //array con 5 clientes

std::vector<Cliente> clientes; //lista para un número indeterminado de clientes

class ListaClientes // clase personalizada para gestionar la lista
{
  // ...
};

On the other hand, note that the variable contador does not belong to the class ... this means that you can only have one list in your entire program. Try to create two lists and fill one with 5 elements ... and then try to use the other, you'll see that laugh. If this variable is used to manage the list, it must be inside the class, not outside.

Unless the exercise is imposing the current design, I would suggest that you modify it as soon as possible.

That said, you're implementing the list as an array of elements (well, there really are 5 arrays ... one for each attribute, an outrage). Based on the design you have, to delete an element you have to:

  • Shifts the following records up one position, so that the record to be deleted is overwritten.
  • Decrease variable contador .

Something like this:

// usando la STL
std::copy(&nombre[indice_a_borrar+1],&nombre[6],&nombre[indice_a_borrar]);
contador--;

// a mano
for( int i = indice_a_borrar+1; i<6; i++ )
  nombre[i-1] = nombre[i];
contador--;    
    
answered by 27.09.2017 в 07:52