为什么我必须按两次Enter键? [英] Why do I have to press enter Twice?

查看:181
本文介绍了为什么我必须按两次Enter键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于某种原因,当我到达某个位置时,必须两次按 Enter 才能提交.我添加了clear来防止它跳过输入,并添加了ignore()来防止它在缓冲区中保留任何多余的字符.我输入我的输入,然后将其下拉至新行,再次按 Enter ,然后输入输入并继续执行程序就没问题了,但我想知道为什么.这是一个代码段:

For some reason in my program when I reach a certain spot, I have to press Enter twice in order to get it to submit. I added the clear to keep it from skipping input and the ignore() to keep it from keeping any extra characters in the buffer. I enter my input and then it drops down to a new line, I hit Enter again and it enter the input and continues the program no problem but I'm wondering why. Here's a code snippet:

    cin.ignore();
    cout << "Enter Student Major (ex. COSC): ";
    cin.getline(student.major, 6);

    for(int i = 0; i < sizeof(student.major); i++)
        student.major[i] = toupper(student.major[i]);

    cin.clear();
    cin.ignore(numeric_limits<streamsize>::max(), '\n');

有什么建议吗?

推荐答案

在我看来,您扔了太多 cin.ignore() ,不知道确切为什么需要它们以及何时将它们放在那里.

It seems to me that you are tossing too many cin.ignore() around, not knowing exactly why they are needed and when to put them there.

在两种常见情况下,需要cin.ignore()才能使输入正常工作":

There are two common circumstances where cin.ignore() is needed to "make input work right":

  1. 混合格式化的和未格式化的输入时;
  2. 从格式化的输入错误中恢复.

在两种情况下,您都希望摆脱输入缓冲区中的虚假字符;如果没有任何这样的字符(这可能是程序中发生的情况),cin.ignore()将暂停执行并等待用户输入-毕竟,您要求它忽略某些字符,并且该死,它将服从它的订单.

In both cases, you want to get rid of spurious characters from the input buffer; if there isn't any such character (which is probably what happens in your program), cin.ignore() will pause the execution and wait for user input - after all, you asked it to ignore some characters, and dammit, it will obey to its orders.

(尽管默认情况下ignore()只会吃掉"一个字符,无论它可能是什么,执行都会暂停直到找到换行符,因为默认情况下cin是行缓冲的-在换行符之前不会检查新输入已收到)

(although ignore() by default would "eat" just one character, whatever it may be, the execution is paused until a newline is found because by default cin is line buffered - new input is not examined until a newline is recieved)

cin.ignore()调用>运算符).

之所以发生这种情况,是因为>>运算符将换行符留在了输入缓冲区中.如果仅执行格式化的输入操作(默认情况下,它们会在尝试解释输入之前跳过所有空格),那么这不是问题,但是如果事后再执行未格式化的输入,则是一个问题:默认情况下,getline会读取直到找到换行符为止,因此左侧的虚假换行符"将使其立即停止读取.

This happens because the >> operator leaves the newline in the input buffer; that's not a problem if you are performing only formatted input operations (by default they skip all the whitespace before trying to interpret the input), but it's a problem if afterwards you do unformatted input: getline by default reads until it finds a newline, so the "spurious newline" left will make it stop reading immediately.

因此,在这里,您通常会在连续执行最后一个格式化输入操作之后,立即调用cin.ignore(...)调用以除去换行符,从而确保输入缓冲区为空.之后,知道您将缓冲区留空了,您可以不用担心直接调用getline.

So, here you will usually call cin.ignore(...) call to get rid of the newline just after the last formatted input operation you do in a row, guaranteeing that the input buffer is empty. Afterwards, you can call getline directly without fear, knowing that you left the buffer empty.

相反,将它放在任何getline之前 并不是一个好主意,就像您在代码中所做的那样,因为可能存在导致该getline的代码路径,输入缓冲区干净,因此ignore调用将阻塞.

It's a bad idea, instead, to put it before any getline, as you seem to do in your code, since there may be code paths that lead to that getline that have the input buffer clean, so the ignore call will block.

istream在格式化的输入操作中遇到错误时,它将不良"字符保留在缓冲区中,因此,如果您重试该操作,则会无休止地陷入困境,因为违规者仍然在那里.常见的clear()/ignore()习惯用法可以解决,从输入缓冲区中删除整个有问题的行.

when istream encounters an error in a formatted input operations, it leaves the "bad" characters in the buffer, so if you retry the operation you get stuck endlessly, since the offenders are still there. The usual clear()/ignore() idiom comes to the rescue, removing the whole offending line from the input buffer.

同样,您不会随意放置clear()/ignore()序列,而只是在从格式化的输入操作(设置流的故障位)中得到输入错误之后才可以.

Again, you don't put the clear()/ignore() sequence at random, but only after you get an input error from a formatted input operation (which sets the failbit of the stream).

现在,除了这些情况之外,很少使用cin.ignore()(除非您实际上想跳过字符);不要为了安全起见"随意散布它,否则会遇到您描述的问题.

Now, aside from these cases, it's uncommon to use cin.ignore() (unless you actually want to skip characters); don't spread it around randomly "just to be safe", because otherwise you will encounter the problem you described.

这篇关于为什么我必须按两次Enter键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆