basically my search function never finds the entered name, and it keeps going infinitely through the file (I checked it with two couts that showed me the same thing)
My doubt is, how can I get the file to read correctly and identify if two character strings are the same, and in case it has gone through the entire file, it does not go through nothing itself and exits the while to get to the if(exists ==0), in order to release the message "The student entered does not exist", thank you very much!
struct alumno
{
char apellidoYNombre[50];
char grado[50];
char dojo[50];
char kata[50];
char kumite[50];
int edad;
};
void buscar()
{
FILE *f;
f = fopen("listado_alumnos.dat","rb");
cout << "\nIngrese el nombre del alumno: ";
char apellidoYNombre[50];
fflush(stdin);
gets(apellidoYNombre);
alumno a;
int existe = 0;
fread(&a, sizeof(alumno), 1, f);
while(!feof(f))
{
if(strcmp(apellidoYNombre, a.apellidoYNombre)==0)
{
cout << "______________________________________" << endl;
cout << "Nombre........: " << a.apellidoYNombre << endl;
cout << "Edad..........: " << a.edad << endl;
cout << "Grado.........: " << a.grado << endl;
cout << "Dojo..........: " << a.dojo << endl;
cout << "Kumite........: " << a.kumite << endl;
cout << "Kata..........: " << a.kata << endl;
cout << "______________________________________" << endl;
existe = 1;
break;
}
fread(&a, sizeof(alumno), 1, f);
}
if(existe==0)
{
cout << "El alumno ingresado no existe" << endl;
}
fclose(f);
return;
}
int main()
{
setlocale(LC_ALL, "spanish");
int opc=0, num1=0, num2=0, edad;
char apellidoYNombre[50], grado[50], dojo[50];
FILE *f;
do
{
cout << "\n________________________" << endl;
cout << "-_-MENU DE OPCIONES-_-" << endl;
cout << "1) Agregar Alumno" << endl;
cout << "2) Borrar Alumno" << endl;
cout << "3) Mostrar lista" << endl;
cout << "4) Buscar Alumno" << endl;
cout << "5) Salir" << endl;
cout << "________________________" << endl;
cout << "Elija una opción: ";
cin >> opc;
switch(opc)
{
case 1:
agregar();
break;
case 2:
eliminar();
break;
case 3:
mostrar();
break;
case 4:
buscar();
break;
case 5:
break;
default:
cout << "\nLa opción ingresada es incorrecta";
break;
}
} while(opc != 5);
getch();
return 0;
}