Reputation: 349
Before you start wasting your time on me, please keep in mind that this question is more about knowing if this shutdown is legit enough.
Alright.
I read all kinds of ways to shut down a program. I KNOW that it's always best to shut down the program, at the end! So as i said, i read all kinds of topics, but i couldn't really find one that i understood correctly. So i kinda came up with my own way. I just want to make sure it's an okay method.
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;
}
So i want you to focus on the while loop. This is my method.
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;
}
}
And this is my command function. As you see, this changes "running" to 0 (or false). I hope i made this understandable enough.
Thanks.
EDIT: All i want to know is, if this is an okay method :)
FINAL EDIT: Alright! I changed "exit(0);" to "return(0);". So i guess this is a okay good method! Thanks for your help! :)
Upvotes: 2
Views: 4448
Reputation: 3654
Given the boundary conditions in the question exit() does what you want it to. It terminates the process normally, performing the regular cleanup for terminating programs (atexit(), destroying objects without automatic storage etc).
You don't really want to use a global running, so I would rather suggest checking the return parameter of commands().
say
int commands()
{
if(wanna_exit)
return 1;
else
return 0;
}
And if you for some reason cannot just let commands() break the while loop (For example by checking the return parameter and setting running to 0 if it is 1), and you want to exit the program instantly rather than finishing evaluating the rest of the while loop then exit() is ok (although in this particular case return is better (since you are in main in the example) ). Just try to avoid having running as global - might seem innocent right now, but can turn mean later :).
Upvotes: 0
Reputation: 17163
Using exit()
in a program is legal. Though it's generally a bad idea. But if the program is stuck without a way back for some weird reason, you can do it.
Using exit
in main()
is rude: I just can't think of a sane reson not to use return instead.
The difference between exit() and return from main is that the former will leave all local objects in stack frames from main to the call point hanging, destructor not called. Only the static/global objects get proper shutdown. It may surprise some portions of the code, and leave important things not done.
As C++ has fine exceptions, I'd consider it preferable to replace exit() with throwing something that manages up to main, where regular return happens.
Upvotes: 4
Reputation: 29265
The if (running == 0)
but is pointless!
while (running == 1) {
commands();
}
return 0;
Does exactly the same - once running is 0 it falls out the bottom of the loop, and main returns. The whole idea of the global running
is getting into side effect programming, which is a bad thing!
Upvotes: 1