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