I am trying to print in the console a list of groups and their assigned teachers in already defined groups with this script:
#include <iostream>
#include <conio.h>
using namespace std;
int main(){
int total_profesores = 2;
char grupos[20] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T'};
struct datosProfesores {
string nombre;
char grupo;
}profesor[total_profesores];
for(int i = 0; i < total_profesores; i++){
cout << "ingresa nombre del maestro" << ": ";
cin >> profesor[i].nombre;
cout << "ingresa el grupo del maestro [" << profesor[i].nombre << "] (A - T): ";
cin >> profesor[i].grupo;
cout << endl << endl;
}
//Es aquí donde tengo el problema
for(int i = 0; i < 20; i++){
if(profesor[i].grupo == grupos[i]){
cout << "Profesor " << profesor[i].nombre << " asignado" << endl;
}else{
cout << "Sin profesor asignado" << endl;
}
}
return 0;
}
I'm quite new to c and would like to show the groups with their respective teachers but I can't find a way to manage the arrangements, any recommendations?
You are describing a case study for which associative containers were designed . In your case, it seems that the best choice is a map whose key is the group and whose value is a list of names in that group:
You can use it like this:
Then you can display the data like this:
You can see the code working in Try it online!.