基本上我首先将一个整数作为输入,然后是测试用例。我的每个测试用例都是一个字符串。如果字符串的起始模式匹配“HI A”并且它不区分大小写,我想要打回字符串。我写下面的代码来完成这个。我的问题是,当我在每次输入后按Enter键时,getline将换行符作为新输入。我试图通过在每次输入后使用额外的getline来解决这个问题,但问题仍然存在。程序卡在循环中,即使我已经设置了休息条件。我做错了什么?
#include <iostream>
#include <string>
using namespace std;
int main(){
int N;
cin >>N;
string nl;
getline(cin,nl);
for (int i=0;i<N;i++){
string s;
getline(cin,s);
//cout <<"string"<<s<<endl;
int flag=0;
if ((s.at(0)=='h'||s.at(0)=='H')&&(s.at(1)=='i'||s.at(1)=='I')&&(s.at(2)==' ')&&(s.at(3)=='a'||s.at(3)=='A')) flag=1;
if (flag==1) cout << s;
//cout << "not " <<s;
string ne;
cout << "i="<< i<<endl;
if (i==N-1) {break;}
getline(cin,ne);
}
}
以下是示例输入:
5
Hi Alex how are you doing
hI dave how are you doing
Good by Alex
hidden agenda
Alex greeted Martha by saying Hi Martha
输出应为:
Hi Alex how are you doing
答案 0 :(得分:5)
ignore()函数可以解决问题。默认情况下,它会丢弃所有输入序列,直到换行符。
也可以指定其他稀释剂和炭限。
http://www.cplusplus.com/reference/istream/istream/ignore/
在你的情况下就是这样。
cin >> N;
cin.ignore();
答案 1 :(得分:4)
您的cin >>N
停在第一个非数字字符处,即换行符。你有一个getline
来阅读它,这很好。
之后的每个额外getline
读取整行,包括最后的换行符。通过输入第二个getline
,你就可以跳过一半的输入。
答案 2 :(得分:2)
所以,你真正的问题不是getline
吃换行,而是你的第二个getline(cin, ne)
吃了一行...
这是因为你错误地认为你需要两个getline
操作来读取一行 - 或类似的东西。混合“基于行”和“迭代”输入确实有令人困惑的方式处理换行,所以你需要一些东西来“跳过”留下frin cin >> N;
后面的换行符,但是一旦你摆脱了它,你只需要一个getline
来读取并在行尾包含换行符。
答案 3 :(得分:1)
你只需要接受这样一个事实,即getline会在最后给你'\ n'。一个解决方案是在获取后删除'\ n'。另一个解决方案是不要写额外的'endl'。例如,对于您的问题,您可以使用此代码
int N;
cin >> N;
string line;
getline(cin, line); // skip the first new line after N.
for (int i = 0; i < N; i++) {
string line;
getline(cin, line);
string first4 = line.substr(0, 4);
// convert to upper case.
std::transform(first4.begin(), first4.end(), first4.begin(), std::ptr_fun<int, int>(std::toupper)); // see http://en.cppreference.com/w/cpp/algorithm/transform
if (first4 == "HI A") {
cout << line; // do not include "<< endl"
}
}
答案 4 :(得分:0)
cin.ignore()
为我工作。
void House::provideRoomName()
{
int noOfRooms;
cout<<"Enter the number of Rooms::";
cin>>noOfRooms;
cout<<endl;
cout<<"Enter name of the Rooms::"<<endl;
cin.ignore();
for(int i=1; i<=noOfRooms; i++)
{
std::string l_roomName;
cout<<"Room"<<"["<<i<<"] Name::";
std::getline(std::cin, l_roomName);
}
}
答案 5 :(得分:0)
我写这个答案的希望是,它可以帮助那些想要一个非常简单的解决方案的人。
在我的情况下,问题出在某些文件的行尾不同,例如“ \ r”和“ \ n”。在Windows中一切正常,但随后在Linux中失败。
答案实际上很简单。读完每一行后,我创建了一个函数removeNewLineChar。这样就删除了char。 removeNewLineChar接收已读入的行,并将其逐个字符地复制到新字符串中,但是避免复制任何换行符。
这里是其工作原理的完整说明。
C++ getline reads in the newline character and how to avoid it