class calcBMI {
public:
string line;
string line2;
fstream search;
short loop = 0;
string weight[6];
string height[6];
int index[6] = { 1, 2, 3, 4, 5 };
int i;
void getHeight();
void getWeight();
};
.
void calcBMI::getWeight() {
search.open("name.txt"); //Opens the text file in which the user details are stored
if (search.is_open())
{
while (getline(search, line) && (getline(search, line2))) { //While the program searches for the lines in the text file
if (line.find("Current Weight(KG): ") != string::npos && (line2.find("Height(Metres)") != string::npos)) { //If the string "Name" isnt the last word on the line
weight[loop] = line; //Assings the strings read from the text file to the array called weight
height[loop] = line2;
cout << index[loop] << ". " << weight[loop] << ", " << height[loop] << endl; //Outputs the index array which loops through the numbers in the array and outputs the weight variable which loops through the strings in the array
loop++; //Iterates the loop
}
}
}
}
So im trying to read two pieces of data from a txt file which contains 5 users. The data stored about the users are their name, height, weight and, previous weights. The layout/format of the file is below.
- Name: Michael
- Current Weight(KG): 65
- Four Previous Weight Measurements: 67, 69, 75, 72
- Height(Metres): 1.7
I'm trying to read the heights and the weights of the users by using the following code, but when i run the code, the program doesnt output anything.
The program should print:
- Current Height(KG): 00, Height(Metres): 00
- Current Height(KG): 00, Height(Metres): 00
- Current Height(KG): 00, Height(Metres): 00
- Current Height(KG): 00, Height(Metres): 00
- Current Height(KG): 00, Height(Metres): 00
However, it prints nothing.