Average is not being stored-C

Asked

Viewed 63 times

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?

2 answers

3


Gustavo, in your code you are iterating 2 times unnecessarily.

I took the loop that calculates the average and put inside the for that makes the readings of the notes.

#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;
    int total_aluno = 2; 

    for(j=0; j<total_aluno; 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);
        // Calculando as médias aqui 
        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<total_aluno; 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");
}

I found the error quite strange, but it seems to me something with the variable reference.

0

Before the fclose(arq); call the fflush(arq);

  • Even so, the average keeps appearing zeroed. The strange thing is to be storing the value 0.00 for it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.