C++替换系统(“暂停”)调用
我读到 system("pause")
速度很慢,不建议使用。
有什么函数可以代替它吗?
我已经尝试过 getchar() ,但如果我之前有过 scanf 调用,它根本不会等待其他输入,只有当我放置另一个 getchar 时() (但我认为这不是一个好的解决方案)。
编辑:我正在使用 Microsoft Visual Studio 2010
I've read that system("pause")
is slow and not advisable to use.
Is there any function that I can use instead of that?
I've tried getchar()
but if I have a scanf
call before, it simply does not waits for an other input, only if I put an other getchar()
under it (but I don't think it's a good solution).
edit: I'm using Microsoft Visual Studio 2010
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
确保在调用之前清空输入缓冲区;否则它可能会获取缓冲区中已经存在的键(例如换行符......)。
Make sure you empty the input buffer before calling it; otherwise it might grab a key that was already in the buffer (like, say, a newline character...).
你尝试过吗?
Have you tried?
conio.h 中的
_getch()
应该是一个足够的替代品。_getch()
from conio.h should prove an adequate replacement.该
system("pause")
调用会被某些 IDE(例如 Bloodshed Dev C++)插入到自动生成的代码中,因为当您从 IDE 运行代码时弹出的控制台窗口是短暂的并且会消失程序一完成。解决方案是不要从 IDE 运行代码,而是打开一个真正的控制台窗口,一旦程序终止,您仍然可以读取程序的输出。
That
system("pause")
call is inserted in autogenerated code by some IDEs (Bloodshed Dev C++ comes to mind) because the console window that pops up when you run the code from the IDE is ephemeral and disappears as soon as the program is done.The solution is to not run the code from the IDE--open a real console window instead, and you'll still be able to read your program's output once it terminates.