【问题标题】:Trying to read two pieces of data from txt file and output them [C++]试图从txt文件中读取两条数据并输出[C++]
【发布时间】:2020-03-30 23:44:21
【问题描述】:

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 文件中读取两条数据。存储的有关用户的数据是他们的姓名、身高、体重和以前的体重。文件的布局/格式如下。

  • 姓名:迈克尔
  • 当前重量(KG):65
  • 之前的四次重量测量:67、69、75、72
  • 高度(米):1.7

我正在尝试使用以下代码读取用户的身高和体重,但是当我运行代码时,程序不输出任何内容。

程序应该打印:

  1. 当前身高(KG):00,身高(米):00
  2. 当前身高(KG):00,身高(米):00
  3. 当前身高(KG):00,身高(米):00
  4. 当前身高(KG):00,身高(米):00
  5. 当前身高(KG):00,身高(米):00

但是,它什么也没打印。

【问题讨论】:

  • 这能回答你的问题吗? Read file line by line using ifstream in C++
  • 你能告诉我你的文件是什么样的吗?
  • getline(search, line) &amp;&amp; (getline(search, line2) 连续两行搜索您想要的数据,如果两次搜索都成功,则只输入if 的正文。在查看了您的输入格式后,your rubber duck 为您提供了一个观察结果:“哇!这两行并没有相邻!”你应该和小鸭坐下来谈谈这件事的含义。
  • 我已经上传了txt文件
  • 我对代码进行了编辑。我相信它会对您的文件起作用。

标签: c++ loops fstream getline


【解决方案1】:

这是重写的版本:

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

【讨论】:

  • if 在这种情况下是没用的。没有从错位文件中恢复的好方法。只需阅读四行并假设它们是正确的,或者编写一个更智能的扫描仪来确保对齐。
  • 它确实有效,但它只输出第一个用户和最后一个跳过用户之间的用户
  • 我重写了它。立即尝试。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多