C++ 等待用户输入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21257544/
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-08-27 23:34:17  来源:igfitidea点击:

C++ wait for user input

c++

提问by Ivars

What would be the best way to wait for user input in console application?

在控制台应用程序中等待用户输入的最佳方法是什么?

std::cout << "press any key to exit...";
// wait for user to hit enter or another key

回答by herohuyongtao

Several ways to do so, here are some possible one-line approaches:

有几种方法可以做到这一点,这里有一些可能的单行方法:

  1. Use getch()(need #include <conio.h>).

  2. Use getchar()(expected for Enter, need #include <iostream>).

  3. Use cin.get()(expected for Enter, need #include <iostream>).

  4. Use system("pause")(need #include <iostream>).

    PS: This method will also print Press any key to continue . . .on the screen. (seems perfect choice for you :))

  1. 使用getch()(需要#include <conio.h>)。

  2. 使用getchar()(预期Enter,需要#include <iostream>)。

  3. 使用cin.get()(预期Enter,需要#include <iostream>)。

  4. 使用system("pause")(需要#include <iostream>)。

    PS:此方法也会Press any key to continue . . .在屏幕上打印。(对你来说似乎是完美的选择:))



Edit:As discussed here, There is no completely portable solution for this. Question 19.1 of the comp.lang.c FAQcovers this in some depth, with solutions for Windows, Unix-like systems, and even MS-DOS and VMS.

编辑:正如这里所讨论的,对此没有完全可移植的解决方案。comp.lang.c FAQ 的问题 19.1对此进行了一定的深入探讨,提供了适用于 Windows、类 Unix 系统甚至 MS-DOS 和 VMS 的解决方案。

回答by CMS

a do while loop would be a nice way to wait for the user input. Like this:

do while 循环将是等待用户输入的好方法。像这样:

int main() 
{

 do 
 {
   cout << '\n' << "Press a key to continue...";
 } while (cin.get() != '\n');

 return 0;
}

You can also use the function system('PAUSE')but I think this is a bit slower and platform dependent

您也可以使用该功能,system('PAUSE')但我认为这有点慢且取决于平台

回答by CashCow

There is no "standard" library function to do this. The standard (perhaps surprisingly) does not actually recognise the concept of a "keyboard", albeit it does have a standard for "console input".

没有“标准”库函数可以做到这一点。该标准(也许令人惊讶)实际上并不承认“键盘”的概念,尽管它确实有“控制台输入”的标准。

There are various ways to achieve it on different operating systems (see herohuyongtao's solution) but it is not portable across all platforms that support keyboard input.

在不同的操作系统上有多种实现方法(参见 herohuyongtao 的解决方案),但它不能跨所有支持键盘输入的平台移植。

Remember that C++ (and C) are devised to be languages that can run on embedded systems that do not have keyboards. (Having said that, an embedded system might not have various other devices that the standard library supports).

请记住,C++(和 C)被设计为可以在没有键盘的嵌入式系统上运行的语言。(话虽如此,嵌入式系统可能没有标准库支持的各种其他设备)。

This matter has been debated for a long time.

这个问题已经争论了很长时间。

回答by JustCode

You can try

你可以试试

#include <iostream>
#include <conio.h>

int main() {

    //some codes

    getch();
    return 0;
}