私のコードでは、キーボードから複数行を読み取る必要がありました。私がここに持っているコードは仕事をします。コードは次のとおりです。
#include <iostream>
using namespace std;
int main()
{
string input;
string line;
cout<< "Enter the input line" << endl;
while (getline(cin, line))
{
if (line == "^D")
break;
input += line;
}
cout<< "The input entered was: "<<endl;
cout<< input<< endl;
}
これを実行した後に得られる出力。
Enter the input line
Hello
World !
The input entered was:
HelloWorld !
問題: ご覧のとおり、getline は Hello World を出力するときに空白を表示します。「Hello World !」として出力されることを確認する方法 「HelloWorld !」ではなく これは、改行が n 個ある場合に発生します。前の行文字列と連結されて出力されます。