How do I manipulate specific information from a TXT file in C++?

Asked

Viewed 41 times

0

The file . txt contains the following lines:

1.55 66.5
1.80 90.2
1.66 65.1
1.70 70.0
1.65 58.8
1.58 53.5
1.72 68.5
1.63 67.9
1.71 69.4
1.67 62.4

I need to average the height, average the weight and show on the screen the highest weight and height. There are 10 lines containing 10 characters in each line. The first column is the height and the second the weight.

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    fstream arquivo;
    string sNomArq,sLinha;
    int iQtdLinhas = 0;
    float fAlt,fPes,fAltMed,fPesMed,fSomAlt,fSomPes;
    sNomArq = "grupo.txt";
    arquivo.open(sNomArq);
    if (arquivo.is_open()){
        while (!arquivo.eof()){
            getline(arquivo,sLinha);

            /* Estou travando na parte de ler o espaço específico da altura/peso em cada linha e 
               manipular esses dados em formato float */
           
            fSomAlt = fSomAlt + fAlt;
            fSomPes = fSomPes + fPes;
            iQtdLinhas++;
        }
        fAltMed = fSomAlt / iQtdLinhas;
        cout << "ALTURA MEDIA = " << fAltMed << endl;
        fPesMed = fSomPes / iQtdLinhas;
        cout << "PESO MEDIO = " << fPesMed << endl;
        fPesMed = 0;
        cout << "MAIOR ALTURA = " << fPesMed << endl;
        fPesMed = 0;
        cout << "MAIOR PESO = " << fPesMed << endl;
    }else{
        cout << "Arquivo nao encontrado" << endl;
    }
    arquivo.close();
    return 0;
}
  • Failure to initialize the cumulative variables fSomAlt and fSomPes with 0. You are reading an entire line, so you need to take the two variables of the string read (you can use the stof function). If any other error has occurred please report it.

  • Thank you very much! I’ve implemented it. Have any idea how to proceed to store the characters of height and weight in different variables sAlt and sPes ? And how to compare the new float variable (string conversion) to see which is the highest value?

  • Why you don’t use Cin to directly read the two variables fAlt and fPes instead of reading the whole line and then separating?

1 answer

0


#include <iostream>
#include <fstream>
#include <string.h>

using namespace std;

int main()
{
    fstream arquivo;
    string sLinha,sAlt,sPes;
    int iQtdLinhas=0;
    float fAlt,fPes,fAltMed,fPesMed,fSomAlt=0,fSomPes=0,fMaxAlt=0,fMaxPes=0;
    arquivo.open("grupo.txt");
    if (arquivo.is_open()){
        while (!arquivo.eof()){
            getline(arquivo,sLinha);
            sAlt = sLinha.substr(0,4);
            sPes = sLinha.substr(6,4);
            fAlt = stof(sAlt);
            fPes = stof(sPes);
            fSomAlt = fSomAlt + fAlt;
            fSomPes = fSomPes + fPes;
            if (fMaxAlt < fAlt){
                fMaxAlt = fAlt;
            }
            if (fMaxPes < fPes){
                fMaxPes = fPes;
            }
            iQtdLinhas++;
        }
        fAltMed = fSomAlt / iQtdLinhas;
        cout << "ALTURA MEDIA = " << fAltMed << endl;
        fPesMed = fSomPes / iQtdLinhas;
        cout << "PESO MEDIO = " << fPesMed << endl;
        cout << "MAIOR ALTURA = " << fMaxAlt << endl;
        cout << "MAIOR PESO = " << fMaxPes << endl;
    }else{
        cout << "Arquivo nao encontrado" << endl;
    }
    arquivo.close();
    return 0;
}

Browser other questions tagged

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