So I have this code that reads 2 files, organizes the data in structs and after is supposed to fill 2 other structs in the bottom function, however the code seems to not even start running the main
function and I have no idea why.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Curso {
char Cod_inst[100];
char Cod_curso[100];
char Nome_inst[100];
char Nome_curso[100];//este
char Grau[100];
int Vagas;
} Curso;
typedef struct Candidato {
int StudentID;
float ProvaIngresso;
float NotaSecundario;
float NotaCandidatura; // >10
int Escolha1, Escolha2, Escolha3, Escolha4, Escolha5;
char curso1[4], curso2[4], curso3[4], curso4[4], curso5[4]; //
} Candidato;
typedef struct Colocados {
int StudentID;
float NotaCandidatura;
int Opcao;
char Instituicao[100];
char Curso[100];
} Colocados;
typedef struct NaoColocados {
int StudentID;
float NotaCandidatura;
} NaoColocados;
void LerCandidatos(Candidato *candidatos);
void LerFaculdades(Curso *cursos);
int ColocaOpcao1(Candidato *candidatos, Curso *cursos, Colocados *Colocado, NaoColocados *NaoColocado, int *flag1);
int main() {
Candidato candidatos[60000];
Curso cursos[5000];
NaoColocados NaoColocado[60000];
Colocados Colocado[60000];
int flag1, flag2, flag3, flag4, flag5;
LerFaculdades(&cursos);
LerCandidatos(&candidatos);
ColocaOpcao1(&candidatos, &cursos, &Colocado, &NaoColocado, &flag1);
printf("%d", flag1);
return 0;
}
void LerFaculdades(Curso *cursos) {
FILE *fl;
char linha[1000];
char *tokencur;
int linha_count = 0;
int n = 0; //token pa controlar codigo intituição;
// Open the CSV file for reading
fl = fopen("Cursos_N05_V02.csv", "r");
if (fl == NULL) {
printf("Error: could not open file\n");
exit(1);
}
// Read each line of the file and extract the string
while (fgets(linha, 1000, fl) != NULL) {
if (linha_count > 0) { // Skip the first line
tokencur = strtok(linha, ",");
strcpy(cursos[n].Cod_inst,tokencur);
printf("\n%s", cursos[n].Cod_inst);
tokencur = strtok(NULL, ",");
strcpy(cursos[n].Cod_curso,tokencur);
printf("\n%s", cursos[n].Cod_curso);
tokencur = strtok(NULL, ",");
strcpy(cursos[n].Nome_inst,tokencur);
printf("\n%s", cursos[n].Nome_inst);
tokencur = strtok(NULL, ",");
strcpy(cursos[n].Nome_curso,tokencur);
printf("\n%s", cursos[n].Nome_curso);
tokencur = strtok(NULL, ",");
strcpy(cursos[n].Grau,tokencur);
printf("\n%s", cursos[n].Grau);
tokencur = strtok(NULL, ",");
cursos[n].Vagas = atoi(tokencur);
printf("\n%d", cursos[n].Vagas);
n++;
}
linha_count++;
}
}
void LerCandidatos(Candidato *candidatos) {
FILE *fp;
char line[100];
char *token;
int line_count = 0;
int i = 0; //token pa controlar numero do candidato;
// Open the CSV file for reading
fp = fopen("Candidatos_N10_C20_O05.csv", "r");
if (fp == NULL) {
printf("Error: could not open file\n");
exit(1);
}
// Read each line of the file and extract the string
while (fgets(line, 100, fp) != NULL) {
if (line_count > 0) { // Skip the first line
token = strtok(line, ",");
candidatos[i].StudentID = atoi(token);
printf("\n%d", candidatos[i].StudentID);
token = strtok(NULL, ",");
candidatos[i].ProvaIngresso = atof(token);
printf("\n%f", candidatos[i].ProvaIngresso);
token = strtok(NULL, ",");
candidatos[i].NotaSecundario = atof(token);
printf("\n%f", candidatos[i].NotaSecundario);
token = strtok(NULL, ",");
candidatos[i].NotaCandidatura = atof(token);
printf("\n%f", candidatos[i].NotaCandidatura);
token = strtok(NULL, ",");
candidatos[i].Escolha1 = atoi(token);
printf("\n%d", candidatos[i].Escolha1);
token = strtok(NULL, ",");
strcpy(candidatos[i].curso1,token);
printf("\n%s", candidatos[i].curso1);
token = strtok(NULL, ",");
candidatos[i].Escolha2 = atoi(token);
printf("\n%d", candidatos[i].Escolha2);
token = strtok(NULL, ",");
strcpy(candidatos[i].curso2,token);
printf("\n%s", candidatos[i].curso2);
token = strtok(NULL, ",");
candidatos[i].Escolha3 = atoi(token);
printf("\n%d", candidatos[i].Escolha3);
token = strtok(NULL, ",");
strcpy(candidatos[i].curso3,token);
printf("\n%s", candidatos[i].curso3);
token = strtok(NULL, ",");
candidatos[i].Escolha4 = atoi(token);
printf("\n%d", candidatos[i].Escolha4);
token = strtok(NULL, ",");
strcpy(candidatos[i].curso4,token);
printf("\n%s", candidatos[i].curso4);
token = strtok(NULL, ",");
candidatos[i].Escolha5 = atoi(token);
printf("\n%d", candidatos[i].Escolha5);
token = strtok(NULL, ",");
strcpy(candidatos[i].curso5,token);
printf("\n%s", candidatos[i].curso5);
i++;
}
line_count++;
}
fclose(fp);
}
int ColocaOpcao1(Candidato *candidatos, Curso *cursos, Colocados *Colocado, NaoColocados *NaoColocado, int *flag1) {
for(int k = 0; k < 60000; k++ /*indicador dos candidatos*/) {
for (int j = 0; j < 50000; j++) {
if (candidatos[k].curso1 == cursos[j].Cod_curso && candidatos[k].NotaCandidatura >= 10) {//encontra um curso
if (cursos[j].Vagas > 0) {
strcpy(Colocado[k].Curso, cursos[j].Nome_curso);
strcpy(Colocado[k].Instituicao, cursos[j].Nome_inst);
Colocado[k].NotaCandidatura = candidatos[k].NotaCandidatura;
Colocado[k].Opcao = 1;
Colocado[k].StudentID = candidatos[k].StudentID;
cursos[j].Vagas --;
*flag1 = 1;
return *flag1;
}
if (cursos[j].Vagas == 0) {
printf("hello");
*flag1 = 1;
return *flag1;
}
}
}//nao encontra curso
NaoColocado[k].NotaCandidatura = candidatos[k].NotaCandidatura;
NaoColocado[k].StudentID = candidatos[k].StudentID;
*flag1 = 0;
return *flag1;
}
}
I tried using test variables in several different places and figured out that while running the code doesn't even reach the main
function.