how to pass the data of the current item in the list to a function?

0

I have this project that I have been working with for a few weeks .. I am working with lists .. students and subjects .. with this function I register a previously registered student:

void inscribirAlumno(Alumno_Inscrito*&listaAlumno_Inscrito,Alumno*listaAlumno,Asignatura*listaAsignatura){
int cedula,id1,i=0;
Alumno_Inscrito*nuevo_Alumno_Inscrito=new Alumno_Inscrito();
cout<<"Digite la cedula del estudiante a inscribir: ";
cin>>cedula;
bool band1=false,band2=false;

Alumno*actualAlumno=new Alumno();
actualAlumno=listaAlumno;
Alumno*aux1;
//BUSQUEDA DEL ESTUDIANTE
while((actualAlumno !=NULL)&&(actualAlumno->ci <= cedula)){
    if(actualAlumno->ci == cedula){
        band1=true;
    }
    aux1=actualAlumno;
    actualAlumno=actualAlumno->siguiente;
}

if(band1==true){
    //REGISTRO DEL ESTUDIANTE
actualAlumno=aux1;
cout<<"\nResgristrando alumno: "<<actualAlumno->nombre;
nuevo_Alumno_Inscrito->nombre=actualAlumno->nombre;
nuevo_Alumno_Inscrito->ci=cedula;

Alumno_Inscrito*aux2=listaAlumno_Inscrito;
Alumno_Inscrito*aux3;

while((aux2 != NULL)&&(aux2->ci < cedula)){
    aux3=aux2;
    aux2=aux2->siguiente    ;
}

if(listaAlumno_Inscrito==aux2){
    listaAlumno_Inscrito=nuevo_Alumno_Inscrito;
}

else{
    aux3->siguiente=nuevo_Alumno_Inscrito;
}

nuevo_Alumno_Inscrito->siguiente=aux2;

cout<<"\nDatos del estudiante "<<nuevo_Alumno_Inscrito->nombre<<" cargados correctamente"<<endl;

//REGISTRO DE MATERIAS

nuevo_Alumno_Inscrito->asigT=registroAsignaturas(listaAlumno_Inscrito,listaAsignatura,i,id1,band2);

}
else{
    cout<<"Estudiante no encontrado"<<endl;
}
system("pause");    
}

With this function nuevo_Alumno_Inscrito->asigT=registroAsignaturas(listaAlumno_Inscrito,listaAsignatura,i,id1,band2); apart from knowing how many subjects he enrolled. Call me to another function to register the subjects:

int registroAsignaturas(Alumno_Inscrito*&nuevo_Alumno_Inscrito,Asignatura*&listaAsignatura,int i,int id1,bool band2){
char band3;
do{
Asignatura*actualAsignatura=new Asignatura();
actualAsignatura=listaAsignatura;
Asignatura*aux1;
cout<<"Materia "<<i+1<<endl;
cout<<"Indique el ID de la Asignatura a buscar: ";cin>>id1;
while((actualAsignatura !=NULL)&&(actualAsignatura->id <= id1)){
    if(actualAsignatura->id == id1){
        band2=true;
    }
    aux1=actualAsignatura;
    actualAsignatura=actualAsignatura->siguiente;
    }

    if(band2==true){
        cout<<"    "<<aux1->nombre<<endl;
        cout<<"DATOS:"<<endl;
        cout<<"ID.: "<<aux1->id;
        cout<<"\nEstudiantes: ["<<aux1->alumT<<"]";
        cout<<endl<<endl;
        nuevo_Alumno_Inscrito->asig[i].nombre=aux1->nombre;
        nuevo_Alumno_Inscrito->asig[i].id=aux1->id;
        aux1->alum[aux1->alumT].nombre=nuevo_Alumno_Inscrito->nombre;
        cout<<"\nnombre: "<<nuevo_Alumno_Inscrito->nombre;
        cout<<"\nMateria "<<nuevo_Alumno_Inscrito->asig[i].nombre<<" agegada correctamente ";
        cout<<"a estudiante "<<aux1->alum[aux1->alumT].nombre;
        aux1->alumT++;
        i++;
        cout<<"\nRegistar la Asignatura: "<<i+1<<"?(s/n) ";
        cin>>band3;

    }
    else{
        cout<<"Asignatura no encontrado"<<endl;
    }
    }while((band3=='s')||(band3=='S')||(i==7));
return i;
}

The problem I have in these lines of code:

if(band2==true){
    cout<<"    "<<aux1->nombre<<endl;
    cout<<"DATOS:"<<endl;
    cout<<"ID.: "<<aux1->id;
    cout<<"\nEstudiantes: ["<<aux1->alumT<<"]";
    cout<<endl<<endl;
    nuevo_Alumno_Inscrito->asig[i].nombre=aux1->nombre;
    nuevo_Alumno_Inscrito->asig[i].id=aux1->id;
    aux1->alum[aux1->alumT].nombre=nuevo_Alumno_Inscrito->nombre;
    cout<<"\nnombre: "<<nuevo_Alumno_Inscrito->nombre;
    cout<<"\nMateria "<<nuevo_Alumno_Inscrito->asig[i].nombre<<" agegada correctamente ";
    cout<<"a estudiante "<<aux1->alum[aux1->alumT].nombre;
    aux1->alumT++;
    i++;
    cout<<"\nRegistar la Asignatura: "<<i+1<<"?(s/n) ";
    cin>>band3;

If the first element of the list is assigned to the subjects ... then if I want to assign it to another element of the list, it is always assigned to the first one .. but if I assign the subjects first to the second element then when assigning it to the first one they are correctly assigned .. for example .. if I register the students Juan and Pedro ... and at the time of registering them I look for Juan in this line cout<<"\nDatos del estudiante "<<nuevo_Alumno_Inscrito->nombre<<" cargados correctamente"<<endl; throws me the name of Juan .. and then in the other function in this line cout<<"\nnombre: "<<nuevo_Alumno_Inscrito->nombre; says juan .. but if after registering to john .. record to pedro .. when looking for it in this line cout<<"\nDatos del estudiante "<<nuevo_Alumno_Inscrito->nombre<<" cargados correctamente"<<endl; comes out .. but in this line of the other function cout<<"\nnombre: "<<nuevo_Alumno_Inscrito->nombre; me Juan leaves ... and the data is saved in Juan and not in Pedro ..

Note: I just proved that joining the second function in the first one if it works well ... but I do not like to work with so much code in a single function ... for the moment I'll leave it like that .. but if I can guide to pass the parameters correctly to the second function would be very useful

    
asked by Marcel Salazar 21.07.2018 в 23:13
source

1 answer

0

Your code, besides being complicated to follow, has multiple failures. To start you are confusing lists with pointers, and is not the same , look at your function inscribirAlumno :

void inscribirAlumno(Alumno_Inscrito*&listaAlumno_Inscrito,Alumno*listaAlumno,Asignatura*listaAsignatura)

You get three parameters that besides being inconsistent in the type (the first one is pointer reference and the next two pointers) are inconsistent in their nomenclature as you call them lista when in reality they are not a list. That confusion has been seen several times in this community, see this thread for more details.

This confusion list-pointer pushes to develop with raw pointers, which is prone to errors, makes the code more complicated to follow and maintain, complicates the implementation and to top it all ... if you want to use lists: do not reinvent the wheel, lists already exist in the standard library.

I could not see your error but ...

In inscribirAlumno memory reserves to then ignore the requested memory, thus creating a meomria leak:

// Pedir memoria para un 'Alumno'
Alumno*actualAlumno=new Alumno();
// Sobrescribir el puntero recién creado con el puntero recibido como argumento de la función.
actualAlumno=listaAlumno;

You have the same failure in registroAsignaturas :

// Pedir memoria para un 'Asignatura'
Asignatura*actualAsignatura=new Asignatura();
// Sobrescribir el puntero recién creado con el puntero recibido como argumento de la función.
actualAsignatura=listaAsignatura;

Also you have a chaos of auxiliary pointers, it is very difficult to be sure of where you are pointing at each moment, even more so when the names of your pointers and variables are not self-explanatory and you should continually review the code to try to understand its objective .

My advice is that you redo all the code in a simpler way, you say that you do not like to work like that with so much code in a single function perfect! you are closer to reaching the principle of sole responsibility , now take it further and create search functions, printing and high of elements, that could simplify your code a lot; surely with that you will understand your own code better and solve the problem.

Unfortunately, I can not help you better.

    
answered by 23.07.2018 в 08:37