Reading multiple lines from a file

I'm writing a program that would calculate BMI's for a set of individuals from a file. My code reads the first line fine, I'm just unclear on how make my code read multiple lines. Here is a snipet:
1
2
3
4
5
6
7
8
9

		
		cout<<"Enter the path to the BMI file you would like to open: ";
		cin>>bmi_user_in;
		ifstream bmifile;
		bmifile.open(bmi_user_in);
		bmifile>>name>>weight1>>height1;
		cout<<"You entered: "<<bmi_user_in<<endl;


As I said, this works fine for one individual. My book explains how to input from a file, just not how to read from multiple lines.

P.S. : This WAS homework. I did not turn in this part of the assignment becasue I'm stuck on this part. I would just like to learn what I did wrong because I'm sure this is essential to learn.
P.S.S: My goal is to take a list of weights and heights to make averages of all the data. I don't think I need help with those formulas, just help on how to input the data.
I had a similar challenge recently and saw that a for loop under the following construction would read and display the entire contents of a single file.
1
2
3
      Mapfile >> endPT;
      for (int lineno = 0; getline (Mapfile,line) && lineno < endPT; lineno++)
          {cout << line << endl;};


I adapted this loop to read or at least take the values for use from only a specific line. I labeled each line in my data so I could tell the code to do this, I imagine you are given similar identifications in your data.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
do {
      filestream >> CharCheck;
      if (CharSel!=CharCheck) 
      { // read name and all stats to misc catching vars.
      //filestream >> Name;
      filestream >> blankHP;
      filestream >> blankATK;
      filestream >> blankDEF;
      filestream >> blankSPD;
      filestream >> blankSKL;
      filestream >> blankWPN;
      }// end if 
      else {
           //filestream >> Name;
           filestream >> User_HP;     
           filestream >> User_ATK;
           filestream >> User_DEF;
           filestream >> User_SPD;
           filestream >> User_SKL;
           filestream >> User_WPN;                      
      }// end else  
   }while(CharSel!=CharCheck);


I will admit that this would work easily for a single iteration, but is inefficient on the whole. However were you to make the above into a function of it's own and simply run it for each of the individuals that you wanted to check it should work well enough.

--edit--
In the second snippet please ignore the filestream>>Name; It was commented out but I have not polished that bit of code yet so as to remove it. My apologies for the confusion.
Last edited on
Thank you for the reply. My date is not labeled. it is formated as(name weight height):

Jan 122.1 74.5
John 190.0 48.5
SpongeBob 5.0 12.5
Julie 120.5 63.6
John 210.5 70.5
Pam 132.1 64.5
Lucy 142.1 71.5

I know there is a more streamlined way to do this that may have to do with where the reading marker sits.
Well, I seemed to have fixed it. This is my new code:
1
2
3
4
5
6
7
8
cout<<"Enter the path to the BMI file you would like to open: ";
		cin>>bmi_user_in;
		ifstream bmifile;
		bmifile.open(bmi_user_in);
		cout<<"You entered: "<<bmi_user_in<<endl;

		while (!bmifile.eof()) {
		bmifile>>name>>weight1>>height1;


What I'm guessing is that after every iteration of the loop, the read marker simply moves down to the next line until it reaches the end of the file and terminates the loop. Can anyone confirm this?
Yes that is exactly what is happening... Remember to close the file.
Thank you for the reply. I have read that it is always good practice to close the file.
Topic archived. No new replies allowed.