This is my data in txt file:
aziz ahmad,50,1.82
syazana rosli,55,1.3
darwina daud,60,1.5
faiz aiman, 65,1. 6
zara zainuddin,58,1.4
muhammad muiz,52,1.9
nabila saari, 47,1.4
This is what i'm working on:
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
struct Student
{
string name;
double weight;
double height;
};
int main()
{
Student studList[7];
double BMI, average=0.0, totalBMI = 0.0;
string category;
ifstream in("studentData.txt");
ofstream out("bmiReport.txt");
for(int i=0; i<7; i++)
{
getline(in, studList[i].name, ',');
in>>studList[i].weight>>studList[i].height;
}
for(int j=0; j<7; j++)
{
BMI= studList[j].weight / (studList[j].height*studList[j].height);
if(BMI>=0.0 && BMI<=18.5)
category="Underweight";
else if(BMI>18.5 && BMI<=23.0)
category="Normal";
else if(BMI>23.0 && BMI<=25.0)
category="Overweight- At Risk";
else if(BMI>25.0 && BMI<=30.0)
category="Overweight- Moderately Obese";
else if(BMI>30.0)
category="Overweight- Severely Obese";
totalBMI += BMI;
out<<"\t\tThe BMI Report"<<endl;
out<<"Name\tWeight(kg)\tHeight(meters)\tBMI\tCategories"<<endl;
out<<setprecision(2)<<fixed;
out<<studList[j].name<<"\t"<<studList[j].weight<<"\t"<<studList[j].height<<"\t"<<BMI<<"\t"<<category<<"\t"<<endl;
}
average = totalBMI/7;
out<<"The average BMI: "<<average;
in.close();
out.close();
}
This is the ouput i got:
The BMI Report
Name Weight(kg) Height(meters) BMI Categories
0.00 0.00 inf Overweight- Severely Obese
The BMI Report
Name Weight(kg) Height(meters) BMI Categories
0.00 0.00 inf Overweight- Severely Obese
The BMI Report
Name Weight(kg) Height(meters) BMI Categories
0.00 0.00 inf Overweight- Severely Obese
The BMI Report
Name Weight(kg) Height(meters) BMI Categories
0.00 0.00 inf Overweight- Severely Obese
The BMI Report
Name Weight(kg) Height(meters) BMI Categories
0.00 0.00 inf Overweight- Severely Obese
The BMI Report
Name Weight(kg) Height(meters) BMI Categories
0.00 0.00 inf Overweight- Severely Obese
The BMI Report
Name Weight(kg) Height(meters) BMI Categories
0.00 0.00 inf Overweight- Severely Obese
The average BMI: inf
As you can see i need to read the data from the file in order to complete the calculation. This output becomes blank as I have included above. Could you please explain on how i can make the coding read the data? Sorry i am beginner in coding. Thank you in advance for helping me.
getline, delimited by',', to separate yournamefrom the remainder of the line. So.. what do you think takes care of that between theweightandheightvalues ? It seems your real question is How can I read and parse a CSV with C++