Before sharing my suggestion with you, I would like to suggest other corrections. In C ++ the keyword struct
is not part of the type, the struct
are already a type per se , so it is not necessary to prefix this keyword to define instances of that data, your definitions of types could look like this:
struct Profesores{
char nombre[30];
double ci;
};
struct Asignaturas{
char nombre[30];
char id[30];
Profesores p_prof;
};
struct Alumno{
char nombre[30];
double ci;
Asignaturas p_asig;
};
Thus, the type Asignaturas
stores an instance of type Profesores
while the type type Alumno
stores an instance of type Asignaturas
.
If you want to store text, the most convenient is to use std::string
, which will allow you to store arbitrary length texts (no limit to 29 characters), your type definitions could look like this:
struct Profesores{
std::string nombre;
double ci;
};
struct Asignaturas{
std::string nombre;
std::string id;
Profesores p_prof;
};
struct Alumno{
std::string nombre;
double ci;
Asignaturas p_asig;
};
On the other hand, your nomenclature is very confusing, you use the plural to refer to unique concepts; There is no number match! It's as if you said " Do you like my car s ? It's that the there !". In general you should use the singular for types and the plural for collections, your type definitions could look like this:
struct Profesor{
std::string nombre;
double ci;
};
struct Asignatura{
std::string nombre;
std::string id;
Profesor p_prof;
};
struct Alumno{
std::string nombre;
double ci;
Asignatura p_asig;
};
Now let's see your statements:
For each student, they will ask how many subjects they want to register, the same for teachers.
If I understand it correctly, teachers and students are exactly the same (they contain subjects) so a priori a different class is not necessary:
struct Asignatura {
std::string nombre;
std::string id;
};
struct Persona {
std::string nombre;
std::string id;
std::vector<Asignatura> asignaturas;
};
The type Persona
can be a student or a teacher, if you need to differentiate them you can use two different types (as you did before) or add a list that internally distinguishes them:
enum Tipo {
Alumno,
Profesor,
};
struct Persona {
Tipo tipo;
std::string nombre;
std::string id;
std::vector<Asignatura> asignaturas;
};
To fulfill the premise that both students and teachers must register subjects, I added a std::vector
of Asignatura
, to register elements you could do something similar to this:
Persona crear_persona()
{
Tipo t;
std::string nombre, id;
// Preguntar si es alumno o profesor
// ...
// Preguntar el nombre
// ...
// Preguntar el id
// ...
return {t, nombre, id};
}
And to add subjects, you could also create an auxiliary function:
Asignatura crear_asignatura()
{
std::string nombre, id;
// Preguntar el nombre
// ...
// Preguntar el id
// ...
return {nombre, id};
}
With these auxiliary functions your code could look like this:
int main() {
int asignaturas;
Persona p = crear_persona();
std::cout << "¿Cuantas asignaturas tiene asignadas " << p.nombre << "?\n";
std::cin >> asignaturas;
for (int asignatura = 0; asignatura != asignaturas; ++asignatura) {
p.asignaturas.push_back(crear_asignatura());
}
return 0;
}