尝试从txt文件中读取两条数据并输出[C++]
Trying to read two pieces of data from txt file and output them [C++]
txt file
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
}
}
}
}
所以我试图从包含 5 个用户的 txt 文件中读取两条数据。存储的有关用户的数据是他们的姓名、身高、体重和以前的体重。文件的 layout/format 如下。
- 姓名:迈克尔
- 目前体重(KG): 65
- 前四次体重测量:67、69、75、72
- 身高(米):1.7
我正在尝试使用以下代码读取用户的身高和体重,但是当我 运行 代码时,程序没有输出任何内容。
程序应该打印:
- 目前身高(KG): 00, 身高(Metres): 00
- 目前身高(KG): 00, 身高(Metres): 00
- 目前身高(KG): 00, 身高(Metres): 00
- 目前身高(KG): 00, 身高(Metres): 00
- 目前身高(KG): 00, 身高(Metres): 00
但是,它什么也不打印。
这是重写的版本:
bool w = false, h = false;
while (getline(search, line))
{
if (line.find("Current Weight(KG):") != string::npos)
{
weight[loop] = line; //Assings the strings read from the text file to the array called weight
w = true;
}
else if (line.find("Height(Metres)") != string::npos)
{
height[loop] = line;
h = true;
}
if (w && h)
{
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
w = false;
h = false;
}
}
txt file
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
}
}
}
}
所以我试图从包含 5 个用户的 txt 文件中读取两条数据。存储的有关用户的数据是他们的姓名、身高、体重和以前的体重。文件的 layout/format 如下。
- 姓名:迈克尔
- 目前体重(KG): 65
- 前四次体重测量:67、69、75、72
- 身高(米):1.7
我正在尝试使用以下代码读取用户的身高和体重,但是当我 运行 代码时,程序没有输出任何内容。
程序应该打印:
- 目前身高(KG): 00, 身高(Metres): 00
- 目前身高(KG): 00, 身高(Metres): 00
- 目前身高(KG): 00, 身高(Metres): 00
- 目前身高(KG): 00, 身高(Metres): 00
- 目前身高(KG): 00, 身高(Metres): 00
但是,它什么也不打印。
这是重写的版本:
bool w = false, h = false;
while (getline(search, line))
{
if (line.find("Current Weight(KG):") != string::npos)
{
weight[loop] = line; //Assings the strings read from the text file to the array called weight
w = true;
}
else if (line.find("Height(Metres)") != string::npos)
{
height[loop] = line;
h = true;
}
if (w && h)
{
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
w = false;
h = false;
}
}