有人向 "gobble a newline" 写东西是什么意思? C++

What does someone mean when they write something to "gobble a newline"? C++

我目前正在学习如何编写代码来提示用户定义他们想要玩骰子游戏的玩家数和回合数,另外一个目标是将两者的结果输出到一个文件中。我看到的一些资源建议在定义字符串变量时,您需要一个辅助字符串,其唯一目的是“吞噬换行符”。

这是我正在查看的代码片段:

int main()
{
    int nPlayers, nRounds, score;
    **string name, dummy;**
    cout <<"Enter number of players: ";
    cin >> nPlayers;
    cout << "Enter number of rounds: ";
    cin >> nRounds;
    **getline (cin, dummy); // gobble up newline**
    ofstream ofs ("scores.txt");
    ofs << nPlayers << " " << nRounds << endl;

我的问题基于用双星号表示的两行。为什么需要这样写一个字符串?

许多输入流在输入之间有额外的换行符。 “Gobble up a newline”就是去掉那些以获得正确的输出。

例如:

5 //number of inputs
//empty newline character
89 //first input value
...

虚拟变量用于存储它,因为它对存储换行符没有多大用处。