【发布时间】:2013-06-26 04:03:41
【问题描述】:
在你开始浪费时间在我身上之前,请记住,这个问题更多的是要知道这次关闭是否足够合法。
好的。
我阅读了各种关闭程序的方法。我知道最后最好关闭程序!正如我所说,我阅读了各种各样的主题,但我真的找不到一个我理解正确的主题。所以我有点想出了我自己的方式。我只是想确保这是一个好的方法。
int main()
{
cout << "Welcome to my fantastic program!" << endl;
cout << "You're the first one to experience my command program!" << endl;
cout << "Let's try with a simple command. Try typing help" << endl;
while (running == 1) {
commands();
if (running == 0) {
exit(0);
}
}
return 0;
}
所以我希望你专注于 while 循环。这是我的方法。
void commands()
{
cin >> command;
if (command == "help")
{
cout << "-------------------------------" << endl;
cout << "-this is the <HELP> section----" << endl;
cout << "-exit (exits the program.)" << endl;
cout << "-Stay tuned for more commands--" << endl;
cout << "-------------------------------" << endl;
}
else if (command == "exit")
{
running = 0;
}
else
{
cout << "The command does not exist: " << command << endl;
}
}
这是我的命令功能。如您所见,这会将“运行”更改为 0(或 false)。我希望我让这个足够理解。
谢谢。
编辑:我只想知道,如果这是一个好的方法:)
最终编辑:好的!我改变了“退出(0);”到“返回(0);”。所以我想这是一个不错的好方法!谢谢你的帮助! :)
【问题讨论】:
-
是的,这是
understandable enough。 -
我的意思是,这是关闭它的合法方式吗?
-
如果是这个问题:是的,
exit()是合法的。 -
而
raise(SIGSEGV)不是! -
为什么不用简单的
return替换exit调用?话虽如此,在这种情况下,退出调用很好。请注意,在更复杂的情况下,如果在堆栈展开期间抛出异常,请调用 exit can lead to a call to terminate。
标签: c++ console-application dev-c++