Getline一直获取换行符。我该如何避免这种情况?

21

基本上,我首先以整数为输入,然后进行测试用例。我的每个测试用例都是一个字符串。如果字符串的起始模式与“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

getline(cin,nl) 更改为 cin.ignore(),以在读取 N 后处理掉该行的其余部分。cin.ignore() 会读取并丢弃从 cin 中仍未读取的该行的其余部分。 - zentrunix
请搜索函数 touppertolower。如果您将字符转换为大写或小写,可以将比较次数减少一半。 - Thomas Matthews
7个回答

15

9
你的 cin >>N 在第一个非数字字符处停止,也就是换行符处。这时你需要使用 getline 读取它,这样很好。
之后每个额外的 getline 都会读取整行,包括末尾的换行符。如果再加上第二个 getline,你就会跳过一半的输入内容。

2
所以,你真正的问题不是getline会吃掉换行符,而是你的第二个getline(cin, ne)会吃掉一行... 这是因为你错误地认为需要两个getline操作来读取一行 - 或者类似的事情。将"基于行"和"基于项目"的输入混合使用确实存在处理换行符时容易混淆的方式,因此你需要跳过由cin>> N;留下的换行符,但一旦你处理好了这个问题,你只需要一个getline来读取包括行末换行符在内的整行内容。

2
我写这篇答案的目的是希望能帮助其他想要解决这个问题的人。在我的情况下,问题是由于某些文件具有不同的行尾符而导致,例如'\r'与'\n'。在Windows中一切正常,但在Linux中失败了。
答案其实很简单。我创建了一个名为"removeNewLineChar"的函数,在每行读取完毕后执行该函数,以此来去除回车符。removeNewLineChar函数接收读取到的行,并将其逐个字符复制到一个新字符串中,但避免复制任何回车符或换行符。
下面是一个示例:
string trim(string line)
{
    string newString;

    for (char ch : line)
    {
        if (ch == '\n' || ch == '\r')
            continue;
        newString += ch;
    }

    return newString;
}


//some function reading a file

while (getline(fin, line)) {
    line = trim(line);
    //... do something with the line
    line = "";
}

这正是我面临的确切问题。非常感谢你。 - I like eating pizza thursdays

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"
  }
}

0
std::string line;
std::cin>>std::ws; // discard new line not processed by cin
std::getline(std::cin,line);

来自注释部分 https://en.cppreference.com/w/cpp/string/basic_string/getline

When consuming whitespace-delimited input (e.g. int n; std::cin >> n;) any whitespace that follows, including a newline character, will be left on the input stream. Then when switching to line-oriented input, the first line retrieved with getline will be just that whitespace. In the likely case that this is unwanted behaviour, possible solutions include:

An explicit extraneous initial call to getline
Removing consecutive whitespace with std::cin >> std::ws
Ignoring all leftover characters on the line of input with cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); 

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);
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接