How to Modify Data Stored in a Vector C ++

0

Good Night, because the next thing is that I do not know how to do to be able to modify the data that is stored in a vector. In my program I ask for the data of workers (Name and Cedula). Then in the end what I want is to Achieve Modify, from example I ask the cedula, and that allows me to change the name or cedula of that person. The code is as follows: (the part that is necessary). How can you perform logical dichar?

Here I Request the Data

for (i = 0; i < 2; i++)
{
    system("cls");
    total_mensual[i] = 0;
    sueldo_mensual[i] = 0;
    seguro[i] = 0;
    bono_mensual[i] = 0;
    sueldo_semana_total[i] = 0;
    cout<<"< = = = = = F A B R I C A de F O R R O S para V E H I C U L O S = = = = = >";
    cout<<"\n\nIngrese el Nombre del Trabajador: "<<i<<". ";
    cin>>nombre[i];
    cout<<"\nCedula del Trabajador: ";
    cin>>cedula[i];

Here as part of a Menu and an Option is where I want to allow me to Modify the Data of a Worker from a given cedula

case 3:

        system("cls");
        cout<<"< = = = = = F A B R I C A de F O R R O S para V E H I C U L O S = = = = = >";
        cout<<"\n\nOpcion 3: Modificar Datos de los Trabajadores";
        cout<<"\n\nIngrese el Numero de Cedula: ";
        cin>>modificar;

        for (i = 0 ; i < 3 ; i++)
        {
            if (modificar == cedula[i])
            {
                cout<<"\n\n------------------------------------";
                cout<<"\n\nNombre del Trabajador: "<<nombre[i];
                cout<<"\n\nCedula: "<<cedula[i];
                cout<<"\n\n------------------------------------";
            }       
        }

        break;
    
asked by Carlos Agustin Guanipa Alvarez 28.04.2017 в 01:59
source

2 answers

2

since you found the record you want to change i indicates that record and you only do cin >> nombre[i]; to change the current value for the new one

for (i = 0 ; i < 3 ; i++)
        {
            if (modificar == cedula[i])
            {
                cout<<"\n\n------------------------------------";
                cout<<"\n\nNombre del Trabajador: "<<nombre[i];
                cout<<"\n\nCedula: "<<cedula[i];
                cout<<"\n\n------------------------------------";
                cout<<"\n\ningrese el nuevo nombre:";
                cin >> nombre[i];
            }       
        }
    
answered by 28.04.2017 / 02:04
source
1

You can do it in the following way by doing another function that returns that position where the card is in the vector:

int buscarcedula (int cedulabuscar, int vectorcedula[]) {
    for (i = 0 ; i < vectorcedula.size() ; i++) {
        if (cedulabuscar == vectorcedula[i]) {
         return i;
        }      
    }
    return -1;
}

This is the implementation on the menu:

case 3:
        cout << "Opcion 3: Modificar Datos de los Trabajadores"<< endl;
        cout << "Ingrese el Número de Cédula: " << endl;
        cin >> cedulamodificar;
        pos = buscarcedula(cedulamodificar, vectorcedula); # (pos) almacena la posision que se encontro la cedula en el vector.
        if ( pos == -1) {
         # Por si se da el caso que no se encuentra la cédula ingresada
          cout << "La cédula indicada no fue encontrada."<< endl;
        } else {
          cout << "Indique lo que quiere modificar de la cédula: " <<cedulamodificar<< endl;
          cout << "1./ Cédula" << endl; # Una vez encontrado pregunta que dado modificar.
          cout << "2./ Nombre" << endl
          cin >> opc;
          if (opc == 1) {
              cout << "Indique la nueva cédula" << endl;
              cin >> vectorcedula[pos]
          } else {
              cout << "Indique el nuevo nombre" << endl;
              cin >> vectornombre[pos]
          }
        }
        break;

I hope you understand what I wanted to do in this code snippet, note that I omitted the declaration of the variables that the sample code uses.

    
answered by 28.04.2017 в 02:31