【问题标题】:How to compare column from a file c++如何比较文件c ++中的列
【发布时间】:2023-04-05 03:05:01
【问题描述】:

我有一个包含 3 列多行的文件。我想比较 2 列的值增加或减少。但是当我比较时,该值总是增加,即使它应该减少。知道如何解决这个问题吗?这是我比较文件中值的编码。但是我没有得到想要的输出。谁能帮帮我?

#include <iostream>
#include <fstream>
using namespace std;
int main()
{

ifstream fromFile;  
fromFile.open("data.txt");
{
    cout << "There was an error" << endl;
    exit(1);
}

else   
  cout << "No error, file opened successfully" << endl;
   

    fromFile.clear();
    fromFile.seekg(0);

    for(int i=1; i<=20; i++)
   {        

    int a{}, b {}, c {};
    fromFile >> a >> b >> c ;
   
    cout<<"  Year : " <<a<<" population : "<<b<<" car : "<<c<< endl;

    if ( b < b && c < c )

    cout << " population decrease and car production decrease " << endl;

    if ( b > b && c > c );
     cout << " population increase and car production increase " << endl;
    
    if ( b > b && c < c )

    cout << " population increase but car production decrease " << endl;

    if ( b < b && c > c );
     cout << " population decrease but car production increase " << endl;
    }

//CLOSES FILES
fromFile.close();
return 0;
}

【问题讨论】:

  • 您正在将变量与自身进行比较...if ( b &lt; b &amp;&amp; c &lt; c )
  • 请将示例输入文件添加到您的minimal reproducible example 并显示所需的输出。

标签: c++ compare


【解决方案1】:

您的代码存在几个问题。我将首先提到这些,然后展示一个更正的解决方案(许多可能的变体之一)。

一些问题:

  • 您不应使用using namespace stdstackoverflow 上有很多解释和解释。改用完整的限定名
  • std::ifstream 有一个自动打开流的构造函数。无需功能open
  • 流有一个重载的bool 运算符。您可以使用if (fromFile) 简单地测试状态
  • 您忘记了if 语句,但有一个else 语句。因此,您的程序将无法编译。而且逻辑上是错误的
  • 由于缺少if,程序总是会立即退出
  • clearseekg 不是必需的。打开流后,文件指针位于开头。
  • 您使用带有固定硬编码幻数 20 的循环。如果文件有更多或更少的行怎么办?您需要使用条件文件结尾来结束循环
  • 您总是将变量与自身进行比较。甚至编译器也会警告你。这行不通。您需要读取一个变量,将其存储在另一个变量中的某个位置,再次从文件中读取并将当前变量与之前的值进行比较。这是您的主要问题
  • 无需调用流的close-函数。当流变量离开其作用域时,析构函数将为您隐式调用它
  • 您没有足够的比较if 语句。很多情况,特别是如果值与以前相同,将不会被处理

那么,我们需要做什么?我现在只用一个变量来解释。让我们过年。我们定义了一个变量year 和第二个变量previousValueYear。在一个循环中,我们将文件中的值读入year。然后,我们将year 变量与previousValueYear 进行比较,进行评估并显示输出。然后,在从文件中读取下一个值之前,我们将当前值从year 复制到previousValueYear。然后我们可以在下一次循环运行时进行正确的比较。

第一个比较有点棘手,因为我们没有previousValueYear。我们将添加一个带有布尔标志的附加 if 语句,以检查此条件。

请在此处查看更正版本:

#include <iostream>
#include <fstream>

int main() {

    // Open the source file
    std::ifstream fromFile("data.txt");

    // Check, if we could open the file
    if (fromFile) {    // bool operator of stream will be called

        // OK, file could be opened. Defineour variables
        int year{}, population{}, car{};
        int previousValueYear{}, previousValuePopulation{}, previousValueCar{};
        bool thisIsTheNotFirstLoopRun{};

        // Read values in a loop and check, if the input worked
        while (fromFile >> year >> population >> car) {

            // Show read data
            std::cout << "Year: " << year << " \tPopulation: " << population << " \tCar: " << car << '\n';

            // In the first loop run, we will not do a comparision (There is only one value and nothing to compare
            if (thisIsTheNotFirstLoopRun) {

                // Do the comparisons
                if ((population < previousValuePopulation) and (car < previousValueCar))
                    std::cout << "Population decrease and car production decrease\n";

                if ((population > previousValuePopulation) and (car > previousValueCar))
                    std::cout << "Population increase and car production increase\n";

                if ((population > previousValuePopulation) and (car < previousValueCar))
                    std::cout << "Population increase and car production decrease\n";

                if ((population < previousValuePopulation) and (car > previousValueCar))
                    std::cout << "Population decrease and car production increase\n";
            }

            // Remember current values for next loop run
            previousValueYear = year;
            previousValuePopulation = population;
            previousValueCar = car;

            // Now, we will not have any longer the first loop run
            thisIsTheNotFirstLoopRun = true;
        }

    } 
    else {
        // Error, file could not be opened. Show message
        std::cerr << "\nError: SourceFile could not be opened\n";
    }

    return 0;
}

高级 C++ 程序员会发生很大变化。不过目前还可以

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-09
    • 2018-02-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多