程序输入只识别单词而不识别行

Program Input only recognizing words not lines

本文关键字:识别 单词 程序 输入      更新时间:2023-10-16

我为一个学校项目编写了这个程序,在这个项目中,用户最多输入4行文本,然后键入"end",然后按相反的顺序向他们重复。它工作得很好,只是它识别出一个"行"只有一个单词,所以如果我尝试键入一行(例如,快速的棕色狐狸跳过日志),它不会将其用作行,而是将每个单词用作行。

#include <iostream>
using namespace std;
#include <string>
int main()
{
        string l1;
        string l2;
        string l3;
        string l4;
        string l5;
        bool end (false);
        while (end == false)
{
        cout<<"Welcome! Type one sentence then press enter, up to 4 sentences. When finished$
        cin >> l1;
        if (l1 == "end")
        {
        cout << "Error! Must enter at least 1 line of text!" << endl;
        break;
        }
        cin >> l2;
        if (l2 == "end")
        {
        cout<<l1<<endl;
        end = true;
        break;
        }
        cin >> l3;
        if (l3 == "end")
        {
        cout<<l2<<endl;
        cout<<l1<<endl;
        end = true;
        break;
        }
        cin >> l4;
        if (l4 == "end")
        {
        cout<<l3<<endl;
        cout<<l2<<endl;
        cout<<l1<<endl;
        end = true;
        break;
        }
        cin >> l5;
        if (l5 == "end")
        {
        cout<<l4<<endl;
        cout<<l3<<endl;
        cout<<l2<<endl;
        cout<<l1<<endl;
        end = true;
        break;
        }
        else
        {
        cout<<"Error! Please enter 4 or less lines"<<endl;
        break;
        }
}
}

使用std::getline而不是operator>>operator>>读取单词,而不是文本行。