C++替换系统(“暂停”)调用

发布于 2024-09-10 20:56:26 字数 207 浏览 6 评论 0原文

我读到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

懒猫 2024-09-17 20:56:26

我尝试过 getchar() 但如果我有
scanf 调用之前,它根本不
等待其他输入

确保在调用之前清空输入缓冲区;否则它可能会获取缓冲区中已经存在的键(例如换行符......)。

I've tried getchar() but if I have a
scanf call before, it simply does not
waits for an other input

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

番薯 2024-09-17 20:56:26

你尝试过吗?

  • 睡眠(时间);
  • 获取();

Have you tried?

  • sleep(int time);
  • getch();
つ低調成傷 2024-09-17 20:56:26

conio.h 中的 _getch() 应该是一个足够的替代品。

_getch() from conio.h should prove an adequate replacement.

被你宠の有点坏 2024-09-17 20:56:26

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文