2
The program below should take the data written in a file . txt, show on screen and do the general average calculation.
the data in . txt are being saved in the following format:
Student: Bruno
First note: 5,00
Second note: 7,00
Average:
6,00
Since everything is saved in string, I need to convert the current average of each student to float in order to do the calculation.
I searched several sites and did not understand how to use the function atof()
so I don’t know if I’m using it properly, and the program isn’t showing on the screen what’s in the file.
If anyone can give me any assistance regarding the use of the function I appreciate.
Follow below my code until the moment.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
int main()
{
setlocale(LC_ALL,"Portuguese");
int i = 0;
double med , media_geral;
char media = "0.00", linha[1000];
FILE *arq;
arq = fopen("alunos.txt", "r");
if(arq == NULL)
{
printf("Arquivo Inexistente");
}
else
{
while((fgets(linha, sizeof(linha), arq))!=NULL)
{
printf("%s", linha[i]);
if (strcmp(linha[i],media))
{
med = atof(linha[i]);
media_geral = media_geral + med;
}
i++;
}
media_geral = media_geral/10;
printf("Média Geral: %.2f", media_geral);
}
fclose(arq);
}
Thank you very much @Victorstafusa ! With your explanation I could see that my if really was useless and that I should study more (especially the concepts of string) Again, thank you very much!
– Bruno Marques