1
Good night!
When running the program and reporting the data Name, Nota1 and Nota2, the program is expected to create a file called "class.txt" with the student’s name, grades and average.
However my problem is reading the average correctly, when opening txt, the average is always 0.00. I wonder how to correct this error.
Thank you.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct
{
char nome[50];
float nota1, nota2,media;
}aluno [10];
int main()
{
FILE *arq;
int j;
float media;
for(j=0; j<10; j++)
{
printf("\n \t Dados do aluno %d", j+1);
printf("\n\n Nome: ");
scanf("%s", aluno[j].nome);
fflush(stdin);
printf("\n Nota 1: ");
scanf ("%f", &aluno[j].nota1);
printf("\n Nota 2: ");
scanf ("%f", &aluno[j].nota2);
}
for (j=0; j<10; j++);
{
aluno[j].media=((aluno[j].nota1)+(aluno[j].nota2))/2;
}
printf("\n\n");
printf("\t Calculando medias e criando arquivo.....");
printf("\n");
arq = fopen("turma.txt","w");
for (j=0; j<10; j++)
{
fprintf(arq,"\n \t Dados do aluno %d \n", j+1);
fprintf(arq,"\n\n Nome: %s, ",aluno[j].nome);
fprintf(arq,"%.2f, ", aluno[j].nota1);
fprintf(arq,"%.2f; ", aluno[j].nota2);
fprintf(arq,"%.2f \n", aluno[j].media);
}
fclose(arq);
system("cls");
system("pause");
}
Have you checked your array
aluno
is being loaded correctly? Students' names always have a single word, i.e., no spaces, for example.:José Roberto
?– anonimo