Problem with If and Vector when printing C ++

1

Good morning, I have the following fragment of an exercise, in which I am sent to print the data of a person that are stored in a vector.

Initially I ask for your ID and immediately you throw me your data. The problem comes when I put a cedula that does not exist or is not stored in the vectors (for this I add an else, indicating that it has not been found), however it comes out several times as can be seen in the image.

case 3:

        do
        {
            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: Consulta de Datos por Cedula";
            cout<<"\n\nIngrese el Numero de Cedula: ";
            cin>>consulta_cedula;

            for (i = 1 ; i <= n ; i++)
            {
                if (consulta_cedula == cedula[i])
                {
                    cout<<"\n\n------------------------------------";
                    cout<<"\n\nNombre del Trabajador: "<<nombre[i];
                    cout<<"\n\nCedula: "<<cedula[i];
                    cout<<fixed<<setprecision(0);
                    cout<<"\n\nForros Producidos en el Mes: "<<total_mensual[i];
                    cout<<fixed<<setprecision(2);
                    cout<<"\n\nTotal a Pagar: Bs. "<<sueldo_mensual[i];
                    cout<<"\n\n------------------------------------";
                }else
                    cout<<"Cedula No Encontrada, Intente Nuevamente";
            }

            cout<<"\n\nDesea Consultar otro Trabajador? (S/N): ";
            cin>>resp;

        } while (resp == 's' || resp == 'S');

        cout<<"\n\n< < < - - - - - - - - - - - - - > > >";
        cout<<"\n\nDesea Volver al Menu? (S/N): ";
        cin>>resp;

        break;

As EXAMPLE, add 3 people (3 data stored in vectors).

IMAGE 1: It correctly shows me the valid cedula with its data, but it leaves 2 times the message of not found, coming from the "else".

IMAGE 2: Here I put an invalid cedula and 3 times it prints the message.

How is this solved? I see that it is due to the FOR but then as I send to print if it is not with the for ...

Greetings!

    
asked by Carlos Agustin Guanipa Alvarez 07.05.2017 в 15:42
source

1 answer

1

Friend, what happens is that you are going through all the cards of the vector, if the program does not find it in iteration 1 of n it will write the message, if it does not find it in the next one it will write the message, and so on .

What you can do is create a boolean flag and at the end of the cycle verify that the cedula is in the vector. In this way:

        bool flag = false;
        int pos = 0;
        for (i = 1 ; i <= n ; i++)
        {
            if (consulta_cedula == cedula[i]){
                flag = true;
                pos = i;
            }
        }
        if (flag){
            cout<<"\n\n------------------------------------";
            cout<<"\n\nNombre del Trabajador: "<<nombre[pos];
            cout<<"\n\nCedula: "<<cedula[pos];
            cout<<fixed<<setprecision(0);
            cout<<"\n\nForros Producidos en el Mes: "<<total_mensual[pos];
            cout<<fixed<<setprecision(2);
            cout<<"\n\nTotal a Pagar: Bs. "<<sueldo_mensual[pos];
            cout<<"\n\n------------------------------------";
        }else{
            cout<<"Cedula No Encontrada, Intente Nuevamente";
        }

The position can be managed in other ways, you can even use another flag to improve the efficiency of your algorithm, do not use a for use a while as I showed you with the search, this would end the cycle when you find the card, if it exists in the vector.

bool flag_cedula = false;
int cont = 0;
//Mientras la cedula no haya sido encontrada y el contador sea menor que 
// n el ciclo continuara, una vez encontrada la cedula el ciclo termina inmediatamente
while(cont < n && !flag_cedula)
    if(consulta_cedula == cedula[i]){
        flag_cedula = true;
    }
    cont++;
}
    
answered by 07.05.2017 / 15:55
source