windows 等到用户在 C++ 中按下 Enter 键?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8177344/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 18:29:41  来源:igfitidea点击:

Wait until user presses enter in C++?

c++windowslinuxcinenter

提问by BobM

waitForEnter() {
    char enter;

    do {
        cin.get(enter);
    } while ( enter != '\n' );
}

It works, but not always. It doesn't work when an enter is pressed just before the function is called.

它有效,但并非总是如此。在调用函数之前按下回车键时它不起作用。

回答by Kerrek SB

You can use getlineto make the program wait for any newline-terminated input:

您可以使用getline让程序等待任何以换行符结尾的输入:

#include <string>
#include <iostream>
#include <limits>

void wait_once()
{
  std::string s;
  std::getline(std::cin, s);
}

In general, you cannot simply "clear" the entire input buffer and ensure that this call will always block. If you knowthat there's previous input that you want to discard, you can add std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');above the getlineto gobble up any left-over characters. However, if there was no extra input to begin with, this will cause an additional pause.

通常,您不能简单地“清除”整个输入缓冲区并确保此调用始终阻塞。如果您知道要丢弃之前的输入,则可以在std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');上方添加getline以吞噬任何剩余的字符。但是,如果开始时没有额外的输入,这将导致额外的暂停。

If you want full control over the console and the keyboard, you may have to look at a platform-specific solution, for instance, a terminal library like ncurses.

如果您想完全控制控制台和键盘,您可能需要查看特定于平台的解决方案,例如像ncurses.

A selectcall on a Posix system that can tell you if reading from a file descriptor would block, so there you could write the function as follows:

一个selectPOSIX的系统调用,它可以告诉你,如果从一个文件描述符会阻塞读,所以有如下,你可以写函数:

#include <sys/select.h>

void wait_clearall()
{
  fd_set p;
  FD_ZERO(&p);
  FD_SET(0, &p);

  timeval t;
  t.tv_sec = t.tv_usec = 0;

  int sr;

  while ((sr = select(1, &p, NULL, NULL, &t)) > 0)
  {
    char buf[1000];
    read(0, buf, 1000);
  }
}

回答by Benjamin Lindley

On Windows, you can do this:

在 Windows 上,您可以这样做:

void WaitForEnter()
{
    // if enter is already pressed, wait for
    // it to be released
    while (GetAsyncKeyState(VK_RETURN) & 0x8000) {}

    // wait for enter to be pressed
    while (!(GetAsyncKeyState(VK_RETURN) & 0x8000)) {}
}

I don't know the equivalent on Linux.

我不知道 Linux 上的等价物。

回答by ejrossi

(the first parameter) The name of the array of type char[]in which the characters read from cinare to be stored.

(第一个参数)要存储char[]读取的字符的类型数组的名称cin

(the second parameter) The maximum number of characters to be read. When the specified maximum has been read, input stops.

(第二个参数)要读取的最大字符数。读取指定的最大值后,输入停止。

(the third parameter) The character that is to stop the input process. You can specify any character here, and the first occurrence of that character will stop the input process.

(第三个参数)停止输入过程的字符。您可以在此处指定任何字符,该字符第一次出现将停止输入过程。

cin.getline( name , MAX, ‘\n' );

Page 175 IVOR HORTON'S BEGINNING VISUAL C++? 2010

第 175 页 IVOR Horton 的可视化C++初见端倪?2010年