从 C++ 退出控制台程序

发布于 2024-08-21 20:54:50 字数 410 浏览 4 评论 0原文

我目前有一个程序,其基本结构

主要功能 如下 -- 向用户显示菜单选项 -- 通过将用户输入传递给第二个函数(input_validator)来验证用户输入 -- 如果用户选择选项 1,则运行功能 1 等

功能 1、2、3 等 -- 从用户请求输入,然后由 input_validator 验证 -- 如果 input_validator 返回 true,我们知道输入是好的

这是我的问题。我想允许用户通过键入“0”随时退出程序。我计划在 input_validator 中使用一些基本代码来完成此操作(如果 input = 0 等)。

这看起来很简单,但有人告诉我,使用 quit() 将导致某些资源从未被释放/等等。我也不能简单地执行“中断”——这将导致我的程序简单地返回到主程序功能。

有什么想法吗?

I currently have a program which has the following basic structure

main function
-- displays menu options to user
-- validates user input by passing it to a second function (input_validator)
-- if user selects option 1, run function 1, etc

function1,2,3,etc
-- input is requested from user and then validated by input_validator
-- if input_validator returns true, we know input is good

Here is my problem. I want to allow the user to quit at any point within the program by typing '0'. I planned on doing this with some basic code in input_validator (if input = 0, etc).

This would appear to be simple, but I have been told that using quit() will result in some resources never been released / etc. I cannot simply do a 'break' either -- it will result in my program simply returning to the main function.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

把昨日还给我 2024-08-28 20:54:50

一种可能性是通过抛出在 main 中捕获的异常来实现此目的,当您捕获它时,您将退出程序。抛出异常的好处是它可以让析构函数运行来清理已创建的对象,如果直接从其他地方退出(例如,使用 exit()),则不会发生这种情况。

One possibility would be to do it by throwing an exception that you catch in main, and when you catch it, you exit the program. The good point of throwing an exception is that it lets destructors run to clean up objects that have been created, which won't happen if you exit directly from elsewhere (e.g., by using exit()).

绮烟 2024-08-28 20:54:50

exit()

正常终止进程,
执行定期清理
终止进程。

首先,注册的所有函数
对 atexit 的调用在
其注册顺序相反。
然后,所有流都被关闭并且
临时文件删除,最后
控制权返回给主机
环境。

exit()

Terminates the process normally,
performing the regular cleanup for
terminating processes.

First, all functions registered by
calls to atexit are executed in the
reverse order of their registration.
Then, all streams are closed and the
temporary files deleted, and finally
the control is returned to the host
environment.

ぽ尐不点ル 2024-08-28 20:54:50

很长一段时间以来,任何一种主流操作系统都不是这样。操作系统确保释放所有内核资源,即使程序没有明确这样做。从代码中的任何位置调用 abort() 或 exit() 都可以。

This hasn't been true for any kind of mainstream operating system for a long time. The OS ensures that all kernel resources are released, even if the program didn't explicitly do so. Calling abort() or exit() from anywhere in your code is fine.

生活了然无味 2024-08-28 20:54:50

exit(int exitCode) - 在 stdlib.h / cstdlib 中定义 - 您可能想要 exit(0); // 正常终止。

exit(int exitCode) - defined in stdlib.h / cstdlib - you'd probably want to exit(0); // normal termintation.

蓝颜夕 2024-08-28 20:54:50

exit() 不会调用析构函数,因此您可能需要考虑使用异常处理程序。

如果您有打开但未刷新的文件之类的内容,操作系统将关闭文件句柄,但不会刷新任何未写入的数据。

exit() will not call your destructors, so you might want to consider using an exception handler instead.

If you have things like open but unflushed files, the OS will close the file handles, but won't flush any unwritten data.

旧人九事 2024-08-28 20:54:50

您必须设计菜单系统,以便可以将状态传递回先前的方法,直到执行 main 函数中的代码为止。类似的问题也适用于后退上一屏幕按钮。

退一步看一下大局,展开技术看起来与 C++ 异常处理策略非常相似。我建议对不遵循正常执行流程的情况使用例外,例如主菜单上一个菜单< /em>.

尝试一下。

You have to design your menu system so that a status can be passed back to the previous method, unwinding until code in the main function is executed. Similar issues apply to back or previous screen buttons.

Taking a step back and looking at the Big Picture, the unwinding technique looks very similar to C++ exception handling strategy. I suggest using exceptions for cases that don't follow the normal flow of execution, such as main menu, and previous menu.

Try it out.

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