【发布时间】: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 < b && c < c ) -
请将示例输入文件添加到您的minimal reproducible example 并显示所需的输出。