1

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.

2
  • You used getline, delimited by ',', to separate your name from the remainder of the line. So.. what do you think takes care of that between the weight and height values ? It seems your real question is How can I read and parse a CSV with C++ Commented Jul 31, 2022 at 17:59
  • share your output and what problem are you facing? Commented Jul 31, 2022 at 18:01

1 Answer 1

1

Maybe this

for(int i=0; i<7; i++)
{
    string temp;
    getline(in, studList[i].name, ',');
    getline(in, temp, ',');
    studList[i].weight = stod(temp);
    getline(in, temp, '\n');
    studList[i].height = stod(temp);
}

Your problem is the commas, so use getline to fix that (you already did it for the name). Then you have a different problem getline only reads strings, so read into a temporary string and use stod to convert the string to the number you want.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.