Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 试图从txt文件中读取两条数据并将其输出[C+;+;]_C++_Loops_Fstream_Getline - Fatal编程技术网

C++ 试图从txt文件中读取两条数据并将其输出[C+;+;]

C++ 试图从txt文件中读取两条数据并将其输出[C+;+;],c++,loops,fstream,getline,C++,Loops,Fstream,Getline,void calcBMI::getWeight(){ search.open(“name.txt”);//打开存储用户详细信息的文本文件 if(search.is_open()) { while(getline(search,line)和&(getline(search,line2)){//,程序在文本文件中搜索行 如果(line.find(“当前重量(KG):”)=string::npos&&(line2.find(“高度(米)”)!=string::npos)){// weight[loop

void calcBMI::getWeight(){
search.open(“name.txt”);//打开存储用户详细信息的文本文件
if(search.is_open())
{
while(getline(search,line)和&(getline(search,line2)){//,程序在文本文件中搜索行
如果(line.find(“当前重量(KG):”)=string::npos&&(line2.find(“高度(米)”)!=string::npos)){//
weight[loop]=line;//将从文本文件读取的字符串分配给名为weight的数组
高度[回路]=第2行;

cout这是一个重写版本:

        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;
            }
        }
bool w=false,h=false;
while(getline(搜索,行))
{
if(line.find(“当前重量(KG):”)=string::npos)
{
weight[loop]=line;//将从文本文件读取的字符串分配给名为weight的数组
w=真;
}
else if(line.find(“高度(米)”)!=string::npos)
{
高度[回路]=直线;
h=真;
}
if(w&h)
{

这能回答你的问题吗?你能告诉我你的文件是什么样子吗?
getline(search,line)和&(getline(search,line2)
搜索两行连续的数据,如果两次搜索都成功,则只进入
的正文。查看输入格式后,有一个观察:“哇!这两条线不是并排的!"你应该和duckie坐下来讨论一下这一点的含义。我已经上传了txt文件,我对代码进行了编辑。我相信它会对你的文件起作用。在这种情况下,
if
是没有用的。没有好的方法从错误对齐的文件中恢复。只要阅读这四行并假设它们是正确的,或者编写一个更智能的scanner以确保对齐。它确实有效,但只输出第一个用户和最后一个跳过中间用户的用户。我重写了它。请立即尝试。
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 

            }

        }

    }

}
        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;
            }
        }