When I search for a student, I get the result twice or return to the previous menu. This is my code:
#include <iostream>
#include <windows.h>
#include <vector>
#include <stdio.h>
#include <conio.h>
#define TOTAL 1000
using namespace std;
struct Alumno
{
char apellido[50];
char nombre[50];
int dni;
int legajo;
};
int Ingresar_Alumno (int &e)
{
FILE *cho;
Alumno vectoralumno[TOTAL];
if (cho=fopen("cho.dat", "wb+"))
{
cout << "ingrese el nombre del alumno: ";
cin >> vectoralumno[e].nombre;
cout << "ingrese el apellido del alumno: ";
cin >> vectoralumno[e].apellido;
cout << "ingrese legajo del alumno: ";
cin >> vectoralumno[e].legajo;
cout << "ingrese el DNI del alumno: ";
cin >> vectoralumno[e].dni;
fwrite(vectoralumno,sizeof(struct Alumno),1,cho);
}
fclose(cho);
e++;
}
void BuscarDNI(int dni, int &q)
{
Alumno dchof;
FILE *x;
if(x=fopen("cho.dat","rb"))
{
fseek(x,0,SEEK_SET);
while (!feof(x))
{
fread(&dchof,sizeof(struct Alumno),1,x);
if(dni == (dchof.dni))
{
Search:
cout << "El alumno buscado es: " << endl;
cout << "Nombre: " << dchof.nombre << endl;
cout << "Apellido: " << dchof.apellido << endl;
cout << "Legajo: " << dchof.legajo << endl;
cout << "DNI: " << dchof.dni << endl;
cout<< "Toque cualquier boton para volver al menu anterior" << endl;
getch();
}
}
if(dni!=dchof.dni)
{
cout<< "Error: DNI Incorrecto" << endl;
cout<< "Toque cualquier boton para volver al menu anterior" << endl;
getch();
}
}
fclose(x);
}
int main()
{
int a,op=0,e=0,q=0;
int dni;
MainMenu:
do
{
system("cls");
cout << "Elija alguna opcion para empezar" << endl;
cout << " " << endl;
cout << "1 - Ingresar nuevo alumno" << endl;
cout << "2 - Buscar alumno" << endl;
cout << "3 - " << endl;
cout << "4 - " << endl;
cout << "5 - " << endl;
cout << "6 - Salir" << endl;
cout << " " << endl;
cout << "Ingrese una opcion deseada: ";
cin >> op;
system("cls");
switch (op)
{
case 1:
Ingresar_Alumno(e);
system("cls");
break;
case 2:
cout<< "Buscar alumno por DNI: ";
cin >> dni;
BuscarDNI(dni,q);
break;
case 3:
break;
case 6:
break;
default:
cout<< "ERROR: OPCION INCORRECTA "<< endl;
getch();
system ("cls");
break;
}
}while(op!=6);
return 0;
}