简体   繁体   English

如何清除cin读取的内容

[英]How to clear the contents read by cin

After typing an integer and pressing ENTER,the newline is getting stored. 输入整数并按Enter后,将存储换行符。
So when i do a char c = cin.get() , the previously entered newline is being assigned to variable c .The following loop gets skipped because of this. 因此,当我执行char c = cin.get() ,先前输入的换行符将分配给变量c 。因此,以下循环被跳过。
.How to clear the contents of cin ? 。如何清除cin的含量? Or how to avoid reading the newline? 还是如何避免阅读换行符?

program: 程序:

char s;
int T;
cin >> T; // pressing ENTER here.
while(T--)
{
  s = cin.get(); // storing the previously pressed ENTER.('\n')
  while(s != '\n') // does not execute.
  {
   .
   .
   .
   .
   .
   .
  }
}
std::cin.ignore();

to ignore one character. 忽略一个字符。

std::cin.ignore(n);

to ignore n characters. 忽略n字符。

std::cin.ignore(std::numeric_limits<std::streamsize>::max());

to ignore all characters. 忽略所有字符。

Optionally, add a second parameter to define a delimiting character. (可选)添加第二个参数以定义定界字符。 The default is EOF. 默认值为EOF。

You have a few options. 您有几种选择。

You could just call cin.getline() , which will retrieve the remainder of the line including the newlines and leave the pointer at the start of the next line. 您可以只调用cin.getline() ,它将检索该行的其余部分,包括换行符,并将指针保留在下一行的开头。 Works as long as you know the remainder of the input line is ignorable. 只要您知道输入行的其余部分可忽略,就可以使用。

You can also use cin.ignore(1) or cin.ignore(2) if you know a newline is next. 如果您知道下一个换行符,也可以使用cin.ignore(1)cin.ignore(2) For input streams that produce CR+LF instead of just one, you'll have to ignore two characters, and it's up to you to figure out if you're getting CR or CR+LF. 对于产生CR + LF而不是一个的输入流,您将不得不忽略两个字符,并且要弄清楚是要获得CR还是CR + LF。

Another option is to use cin.peek() to check what the next character is ahead of time. 另一种选择是使用cin.peek()提前检查下一个字符是什么。 That way you can use skip newlines without consuming non-newline characters. 这样,您可以使用跳过换行符而无需使用非换行符。 For example: 例如:

void skipNewlines (istream &in) {
    int ch;
    while ((ch = in.peek()) == '\r' || ch == '\n')
       in.ignore();
    // and now newlines skipped and next in.get() will read the next char.
}

You could modify that to skip all whitespace. 您可以修改它以跳过所有空格。 That option probably works the smoothest with your use of cin >> T . 使用cin >> T该选项可能最cin >> T

You could also consider writing your own character input function that reads the next character but ignores line breaks (or whatever). 您也可以考虑编写自己的字符输入函数,该函数读取下一个字符,但忽略换行符(或其他任何字符)。 For example: 例如:

int getWithoutNewline (istream &in) {
    int ch;
    do {
        ch = in.get();
    } while (ch == '\n' || ch == '\r');
    return ch;
}

Then use getWithoutNewline(cin) instead of cin.get() to read characters when parsing your integers. 然后在解析整数时使用getWithoutNewline(cin)而不是cin.get()来读取字符。 You have flexibility there; 您在那里有灵活性; you could eg translate the newlines to spaces. 您可以例如将换行符转换为空格。 This has the advantage of being able to operate without you having to know anything about the location or form of the line breaks. 这样的优点是无需您知道换行的位置或形式就可以进行操作。 However, it may not work for all situations. 但是,它可能不适用于所有情况。

Personally, I wouldn't take the approach of just blindly discarding all contents. 就个人而言,我不会盲目丢弃所有内容。 I'm not even sure if that's reliably possible given that there's no end-of-file after each line. 考虑到每一行之后都没有文件结尾,我什至不确定这是否可靠。 The closest approximation to what you're asking for, as far as this goes, is to just call getline() to read and consume the rest of the line (including the newlines). 就此而言,最接近您要求的是,只需调用getline()即可读取和使用其余的行(包括换行符)。

Pick whichever one of those, or some variant of one of those, suits your application and usage the best. 选择其中一种,或其中一种的某种变体,最适合您的应用程序和用法。

The answer from @zenith is correct. @zenith的答案是正确的。 In addition to that, also look at std::cin.peek() . 除此之外,还请查看std::cin.peek()

Here's some code: 这是一些代码:

char s;
int T;
cin >> T; // pressing ENTER here.
while(T--)
{
  while(std::isspace(cin.peek())) // peek used here
    cin.ignore(); // ignore spaces, tabs and enter

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM