【问题标题】:Is there a standard getch() equivalent in C++?C++ 中是否有标准的 getch() 等价物?
【发布时间】:2013-07-22 11:23:21
【问题描述】:

我知道 getch 不是标准的 C/C++ 函数,但我倾向于喜欢它,因为它不需要您在返回之前按 Enter。所以我想知道标准 C++ 中是否有任何具有相同效果的等效项(不需要您按 Enter 键)?

我在这个网站上读过类似的问题,但他们的答案都没有说明是否有标准的和可移植的等价物。

【问题讨论】:

  • 不,没有。
  • 如果有这样的功能,你可能会在你说你读过的一些答案中看到它。
  • 我认为很难将其纳入标准,因为标准并未说明控制台何时将数据提供给您的程序。
  • 为什么不直接使用 fopen(stdin) 和 fgetc() 的组合?
  • 使用 cin.get();
    建议 stackoverflow.com/a/4456902/3645550"> 这里

标签: c++


【解决方案1】:

如果您使用“诅咒”库之一,例如ncurses,则有一个可移植的等价物

【讨论】:

  • 谢谢@Mats Petersson
【解决方案2】:

使用ncurses。这是目前最好、最简单的替代方案。

示例代码:

#include <ncurses.h>

 int main()
 {  
   initscr();                   /* Start curses mode          */
   printw("Hello World !!!");   /* Print Hello World          */
   refresh();                   /* Print it on to the real screen */
   getch();                     /* Wait for user input */
   endwin();                    /* End curses mode        */

   return 0;
 }

【讨论】:

    猜你喜欢
    • 2013-07-19
    • 1970-01-01
    • 2013-06-21
    • 1970-01-01
    • 2012-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-20
    相关资源
    最近更新 更多