Help with class


I have two class of student classes and subjects that are these:

class Estudiante{

private:

int id;
char nombre[TAM];
char apellido[TAM];
int edad;



public:

Estudiante();
Estudiante(int,char[TAM], char[TAM], int);
void mostrardatos();

};


class Materia{

private:

int id;
char nombremateria[TAM];
char profesor[TAM];
Estudiante *lstEstudiante;




public:

Materia();
Materia(int,char[TAM], char[TAM]);
void setlistaestudiante(Estudiante *e);
Estudiante *getlista();
void mostrarinfo();


};

this is menu to program

Build the following menu:
1. Add students
2. Add subject
3. Enroll students to the subject
4. Show subject and its list of students
5. Exit
When selecting option 1, you must allow the user to fill in the data of the object of type
Student, storing this in an array of the same type (Vector size 15)
Option two will allow you to fill in the data of the object of type Materia
Option three should link the list of students with the subject matter created,
using the method setListaEstudiante.
Option 4 should show the details of the subject using the ShowInfo method and
must display the individual data of each student contained in the list,
taking advantage of the getLista method.
Option 5, exit the program

i have problem with option 3 and 4 this function to user typing information


void ingresarEstudiante(){
cout<<"Control de estudiante : Registro "<<endl;
cout<<"--------------------------------------- "<<endl;
cout<<"Ingresar id ";
cin>>id_estudiante;
cout<<"Ingrese nombre ";
cin>>nombbre;
cout<<"Ingrese apellido ";
cin>>apellido;
cout<<"Ingresar edad ";
cin>>edad;

student= new Estudiante(id_estudiante,nombbre,apellido,edad);

lst[contador]=*student;

contador++;

}


void ingresarMateria(){



cout<<"Control de Materias : Registro "<<endl;
cout<<"--------------------------------------- "<<endl;
cout<<"Ingresar id ";
cin>>id_materia;
cin.ignore();
cout<<"Ingrese nombre de la materia ";
cin>>nombremateria;
cout<<"Ingrese Profesor ";
cin>>profesor;


mater= new Materia(id_materia,nombremateria,profesor);


lstm[contador_materia]=*mater;


contador_materia++;




}
and i think to enroll student to the subject i'ts this function but return compilation error

void inscribir(){



cout<<"Nombre de la materia "<<nombremateria<<endl;

for(int i=0;i<contador;i++){
lst[i].mostrardatos();
lstm[i].setlistaestudiante(lst);


}

}


I hope your help, in advance thank you



> but return compilation error
¿what compilation error?
as you have copy-paste some random bits of your code we can't try to compile to get the same errors as you, so at least have the decency to provide the error messages.


> void ingresarEstudiante(){
¿is this a free function or a member function of who knows what class?
you refer to some variables but I can't see their definitions anywhere, ¿are they global?

1
2
student= new Estudiante(id_estudiante,nombbre,apellido,edad);
lst[contador]=*student;
memory leak


¿what's your `list' implementation? ¿just an array? ¿there may be only one subject?
The instructions appear to suggest you keep a global list of up to 15 students.

1
2
3
constexpr int MAX_NUM_DE_ESTUDIANTES = 15;
Estudiante estudiantes[MAX_NUM_DE_ESTUDIANTES];
int NumDeEstudiantes = 0;

I think a similar pattern could be used for the list of subject matter:

1
2
3
constexpr int MAX_NUM_DE_MATERIAS = 15;
Materia materias[MAX_NUM_DE_MATERIAS];
int NumDeMaterias = 0;


Now, to ADD a student, for example, you just need to check that you aren’t at your max, and then get the information from the user:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
void AgregaEstudiante()
{
  // Make sure there is room for the new student’s information
  if (NumDeEstudiantes >= MAX_NUM_DE_ESTUDIANTES)
  {
    std::cout << "No puede agregar más de " << MAX_NUM_DE_ESTUDIANTES << " estudiantes.\n";
    return;
  }

  // Get the new student’s information from the user
  std::cout << "Apellido? ";
  std::cin.getline( estudiantes[NumDeEstudiantes].apellido, TAM );

  std::cout << "Nombre de pila? ";
  ...

  std::cout << "ID? ";
  std::cin >> estudiantes[NumDeEstudiantes].id;
  std::cin.ignore( std::numeric_limits <std::streamsize> ::max(), '\n' );

  ...

  // Number of students’ information has increased
  NumDeEstudiantes += 1;
}


For option three, the user should type in a course name and some piece of information to identify a student (ID? names?). If the user does not type something in correctly, reject it (and make him choose to try from the main menu again).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
void InscribirEstudiante()
{
  std::string s;
  int m, e;

  // Ask for a course name
  std::cout << "Elige curso: ";
  getline( std::cin, s );

  // Find the course
  for (m = 0; m < NumDeMaterias; m++)
    if (s == materias[n].ObtenerNombre())
      break;

  if (m == NumDeMaterias)
  {
    std::cout << "No existe tal curso\n";
    return;
  }
  
  // Ask for a student name/id/whatever
  std::cout << "Elige estudiante por whatever: ";
  ...

  // Find the student
  for (e = 0; e < NumDeEstudiantes; e++)
    if (e == estudiantes[e].whatever)
      break;

  if (e == NumDeEstudiantes)
  {
    std::cout << "No existe tal estudiante.\n";
    return;
  }

  materias[m].agregaEstudiante( @estudiantes[e] );
}


Each course should have more than one student. Make sure you have an array:

1
2
3
4
5
6
7
8
9
class Materia
{
  ...
private:
  ...
  Estudiante* lista_de_estudiantes[MAX_NUM_DE_ESTUDIANTES];
  int num_de_estudiantes;

  ...

Make sure to construct your Materia with an empty list (num_de_estudiantes = 0). The method to add a student to a course is very much the same as the method to add a student to your global list of students, except now you only need to assign a pointer to the student's datos in the global list:

1
2
3
4
5
6
7
8
9
10
11
void Materia.agregaEstudiante( Estudiante* estudiante )
{
  if (num_de_estudiantes >= MAX_NUM_DE_ESTUDIANTES)
  {
    std::cout << "El curso se llenó. No puede agregar más estudiantes.\n";
    return;
  }

  lista_de_estudiantes[num_de_estudiantes] = estudiante;
  num_de_estudiantes += 1;
}

This isn’t the only way to do this kind of thing. You could also maintain a std::vector of students, or pointers to students. You could new[] and delete[] a list of students (which would be a worse pain than using an array). You could maintain a linked list of students.

I suspect your assignment will allow for using an array (or a vector, if you can).


Finally, to display a course and the list of students attached is just iterating over that particular course’s array of students:

1
2
3
4
5
6
7
8
void Materia.mostrarinfo()
{
  std::cout << "Curso: " << nombremateria << "\n";
  std::cout << "Profesor: " << profesor << "\n";
  std::cout << "Estudiantes matriculados:\n";
  for (int n = 0; n < num_de_estudiantes; n++)
    lista_de_estudiantes[n].mostrardatos();
}

The examples I have given you should help you to write code that works. Good luck!
Last edited on
Topic archived. No new replies allowed.