C ++等待用户输入 [英] C++ wait for user input
问题描述
在控制台应用程序中等待用户输入的最佳方式是什么?
std :: cout< 按任何一个键退出...;
//等待用户按Enter或另一个键
有几种方法可以这样做,这里有一些可能的一行方法:
-
使用
getch()
(需要#include< conio.h>
)。 -
使用
getchar()
(预计输入,需要#include< iostream>
)。 -
使用
cin.get()
/ kbd>,需要#include< iostream>
)。 -
c $ c>系统(暂停) (需要
#include< iostream>
)。
$ bPS:此方法也将打印
按任意键继续。 。 。
。 (似乎是你的完美选择:))
<编辑:如此处所述,没有完全可移植的解决方案。 comp.lang.c常见问题的问题19.1涵盖了这一点,包括Windows,类Unix系统甚至MS的解决方案-DOS和VMS。
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
Several ways to do so, here are some possible one-line approaches:
Use
getch()
(need#include <conio.h>
).Use
getchar()
(expected for Enter, need#include <iostream>
).Use
cin.get()
(expected for Enter, need#include <iostream>
).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 :))
Edit: As discussed here, There is no completely portable solution for this. Question 19.1 of the comp.lang.c FAQ covers this in some depth, with solutions for Windows, Unix-like systems, and even MS-DOS and VMS.
这篇关于C ++等待用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!