Exit a Program

Nov 7, 2013 at 1:25am
Hello. How do I exit a program. What is the code so that when a variable (of my choosing) is entered, The program displays a message and quits? It is probably really obvious and I probably use it everyday, but I am stupid. Any help would be appreciated. Thank you!
Nov 7, 2013 at 1:39am
There are multiple ways to exit a program, some graceful, some not, but the most proper way is just to return from main.
Nov 7, 2013 at 1:40am
So what would the code look like in an example?
Nov 7, 2013 at 1:42am
return 0;
Nov 7, 2013 at 1:49am
Thank you. But on the inside of a function, will this work to close the whole program?
For example, if I am in the middle of a function in the middle of all the code and I want the user to enter whether or not they want to make the whole program stop and quit, will this work?
Nov 7, 2013 at 2:16am
No. If you have to do that, you have a design flaw, but if you can't fix the design flaw the next best thing is to use std::exit.

http://www.cplusplus.com/reference/cstdlib/exit/
http://en.cppreference.com/w/cpp/utility/program/exit
Topic archived. No new replies allowed.