This program should get in input a number and calculate the average value between a student's marks. This program works only with the former student, but not with the following ones. I think there's a mistake with that fscanf. Can anybody help?
int main ()
{
FILE *cfPtr;
int matricola_in, matricola, nEsami, codEsame, voto;
char nome[20], cognome[20];
int i, j, trovato = 0;
float somma = 0.0;
printf("Inserisci una matricola:\n");
scanf("%d", &matricola_in);
if( (cfPtr = fopen("studenti.txt", "r")) == NULL )
printf("Errore");
while( !feof(cfPtr) || trovato != 1 ){
fscanf(cfPtr, "%d%s%s%d\n", &matricola, nome, cognome, &nEsami);
if( matricola_in == matricola ){
trovato = 1;
for( i = 0; i < nEsami; i++ ){
fscanf(cfPtr, "%d%d\n", &codEsame, &voto);
somma += voto;
}
printf("Media: %.1f\n", somma/nEsami);
}
}
fclose(cfPtr);
return 0;
}
Edit: the data looks like:
matricola nome cognome n.esami`<eol>`
(for n.esami-rows)codice esame voto`<eol>`
...