while 和 if 使用 getline() 时的区别
Difference between while and if when using getline()
我有一个 .cpp 文件,它从两个 .txt 文件中读取并将它们输出到屏幕上。
这是代码:
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
using namespace std;
class check
{
public:
void display();
ifstream read_number, read_amount;
};
void check::display()
{
read_number.open("number.txt");
read_amount.open("amount.txt");
if (read_number.is_open() && read_amount.is_open())
{
string line;
bool transfer = false;
while (getline(read_number, line) && transfer == false)
{
cout << line << "\t";
transfer = true;
if (getline(read_amount, line) && transfer == true)
{
cout << "$" << line << endl;
transfer = false;
}
}
cout << endl;
}
else cout << "Unable to open one of the two files." << endl;
}
int main()
{
check obj;
obj.display();
return 0;
}
我正在使用“transfer”bool 变量作为从文件中读取一行然后从第二个文件中读取另一行的方法。
第一个 .txt 文件只有这些数字,没有别的:
03
32
26
第二个 .txt 文件有这些数字:
50.30
15.26
20.36
以上代码按预期工作。当 运行:
时,它会产生以下结果
03 .30 // first value is from file 1, second value from file 2.
32 .26
26 .36
但是,我的问题是,为什么以下更改会导致输出中省略最后一个值 (20.36)?
更改:
while (getline(read_amount, line) && transfer == true)
{
cout << "$" << line << endl;
transfer = false;
}
更改产生的输出:
03 .30 // first value is from file 1, second value from file 2.
32 .26
26
如果你们需要对我的问题进行任何澄清,请告诉我。任何帮助,将不胜感激。谢谢!
while
是循环语句,所以循环执行。
让我们看看会发生什么step-by-step:
transfer = true; // (1)
while (getline(read_amount, line) && transfer == true) // (2)
{
cout << "$" << line << endl; // (3)
transfer = false; // (4)
}
transfer
在第 (1) 行设置为 true
- 在第 (2) 行
line
有内容被读取
- 进入循环内部,因为
transfer
在第 (2) 行 true
- 第 (3) 行打印了一些东西
transfer
在第 (4) 行设置为 false
- 在第 (2) 行
line
有内容被读取
- 中断循环,因为
transfer
在第 (2) 行 false
在这一步中,读了两次,它会从输入中消耗一些东西。
另一方面,使用 if
,执行不会循环,只读取一次。
这将有所作为。
我有一个 .cpp 文件,它从两个 .txt 文件中读取并将它们输出到屏幕上。 这是代码:
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
using namespace std;
class check
{
public:
void display();
ifstream read_number, read_amount;
};
void check::display()
{
read_number.open("number.txt");
read_amount.open("amount.txt");
if (read_number.is_open() && read_amount.is_open())
{
string line;
bool transfer = false;
while (getline(read_number, line) && transfer == false)
{
cout << line << "\t";
transfer = true;
if (getline(read_amount, line) && transfer == true)
{
cout << "$" << line << endl;
transfer = false;
}
}
cout << endl;
}
else cout << "Unable to open one of the two files." << endl;
}
int main()
{
check obj;
obj.display();
return 0;
}
我正在使用“transfer”bool 变量作为从文件中读取一行然后从第二个文件中读取另一行的方法。 第一个 .txt 文件只有这些数字,没有别的:
03
32
26
第二个 .txt 文件有这些数字:
50.30
15.26
20.36
以上代码按预期工作。当 运行:
时,它会产生以下结果03 .30 // first value is from file 1, second value from file 2.
32 .26
26 .36
但是,我的问题是,为什么以下更改会导致输出中省略最后一个值 (20.36)?
更改:
while (getline(read_amount, line) && transfer == true)
{
cout << "$" << line << endl;
transfer = false;
}
更改产生的输出:
03 .30 // first value is from file 1, second value from file 2.
32 .26
26
如果你们需要对我的问题进行任何澄清,请告诉我。任何帮助,将不胜感激。谢谢!
while
是循环语句,所以循环执行。
让我们看看会发生什么step-by-step:
transfer = true; // (1)
while (getline(read_amount, line) && transfer == true) // (2)
{
cout << "$" << line << endl; // (3)
transfer = false; // (4)
}
transfer
在第 (1) 行设置为 - 在第 (2) 行
line
有内容被读取
- 进入循环内部,因为
transfer
在第 (2) 行true
- 第 (3) 行打印了一些东西
transfer
在第 (4) 行设置为 - 在第 (2) 行
line
有内容被读取
- 中断循环,因为
transfer
在第 (2) 行false
true
false
在这一步中,读了两次,它会从输入中消耗一些东西。
另一方面,使用 if
,执行不会循环,只读取一次。
这将有所作为。