C ++等待用户输入 [英] C++ wait for user input

查看:573
本文介绍了C ++等待用户输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  std :: cout< 按任何一个键退出...; 
//等待用户按Enter或另一个键


解决方案

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


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


  2. 使用 getchar()(预计输入,需要 #include< iostream> )。


  3. 使用 cin.get() / kbd>,需要 #include< iostream> )。


  4. c $ c>系统(暂停) (需要 #include< iostream> )。
    $ b

    PS:此方法也将打印按任意键继续。 。 。。 (似乎是你的完美选择:))







<编辑:如此处所述,没有完全可移植的解决方案。 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:

  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 :))


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屋!

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