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!