程序输入只识别单词而不识别行
Program Input only recognizing words not lines
我为一个学校项目编写了这个程序,在这个项目中,用户最多输入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>>
读取单词,而不是文本行。
相关文章:
- 文本文件中的单词链表
- 提升 ASIO 无法识别计时器对象
- 从udp接收帧对于人脸识别来说太慢
- 在指针的帮助下,文本文件中单词的频率
- 模板类无法识别友元运算符
- 如何根据单词在文本中出现的概率输出单词
- 从字符串变量中逐字符读取单词
- 使用std::mt19937从字符串中返回一个随机单词
- std::visit无法识别类型
- 如何用for循环在c++中生成单词三角形
- 将数字转换为单词
- 使用if-else将数字转换为单词
- C++-字符串是否包含一个带有简单循环的单词
- 使用单词"not"作为C ++类的名称会导致VS2019错误
- 无法识别模板功能
- 我有两个类需要在同一 cpp 文件中相互引用,但第一个类无法识别第二个类类型的对象
- 如何识别输入的输入字符串在 c++ 中是两个还是多个单词
- Xcode 无法识别单词"class"
- 程序输入只识别单词而不识别行
- 语音识别小词汇量(约20个单词)