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
andfSomPes
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.– anonimo
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
andsPes
? And how to compare the new float variable (string conversion) to see which is the highest value?– Henrique Gumieri
Why you don’t use Cin to directly read the two variables
fAlt
andfPes
instead of reading the whole line and then separating?– anonimo