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.
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.
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?