Convert File string to Float

Asked

Viewed 665 times

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);
}

1 answer

2


You don’t seem to know what a character is and what a string is.

In particular, this if has no sense:

            if (strcmp(linha[i],media))

It turns out that linha[i] is a line character. This character will be interpreted as if it were a pointer and pointing somewhere that it should not point to.

If there wasn’t one if and the file read line is "0.00", the body content of the if would add up to zero the overall average, ie would do no harm. So it is concluded that this if is useless.

Other places where you confuse character with string is in these lines:

            printf("%s", linha[i]);
                med = atof(linha[i]);

I guess just use linha that would be enough.

Finally, the average is calculated by dividing the sum of the terms by the amount of terms. Only you have fixed that the quantity is always 10. The right would be divided by i.

If I understand your problem right, your code looks like this:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>

int main() {
    setlocale(LC_ALL,"Portuguese");
    double media_geral = 0.0;
    char linha[1000];

    FILE *arq;
    arq = fopen("alunos.txt", "r");
    if (arq == NULL) {
        printf("Arquivo Inexistente");
    } else {
        int i = 0;
        while ((fgets(linha, sizeof(linha), arq)) != NULL) {
            printf("%s", linha);
            media_geral += atof(linha);
            i++;
        }
        if (i != 0) media_geral /= i;
        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!

Browser other questions tagged

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