I would like my C++ code to stop running if a certain condition is met, but I'm not sure how to do that. So just at any point if an if
statement is true terminate the code like this:
if (x==1)
{
kill code;
}
This question is related to
c++
The program will terminate when the execution flow reaches the end of the main function.
To terminate it before then, you can use the exit(int status) function, where status is a value returned to whatever started the program. 0 normally indicates a non-error state
return 0;
put that wherever you want within int main()
and the program will immediately close.
Call the std::exit
function.
People are saying "call exit(return code)," but this is bad form. In small programs it is fine, but there are a number of issues with this:
Really, the only time you should exit the problem is with this line in main.cpp:
return 0;
If you are using exit() to handle errors, you should learn about exceptions (and nesting exceptions), as a much more elegant and safe method.
If the condition I'm testing for is really bad news, I do this:
*(int*) NULL= 0;
This gives me a nice coredump from where I can examine the situation.
Beyond calling exit(error_code) - which calls atexit handlers, but not RAII destructors, etc. - more and more I am using exceptions.
More and more my main program looks like
int main(int argc, char** argv)
{
try {
exit( secondary_main(argc, argv );
}
catch(...) {
// optionally, print something like "unexpected or unknown exception caught by main"
exit(1);
}
}
where secondary_main in where all the stuff that was originally is put -- i.e. the original main is renamed secondary_main, and the stub main above is added. This is just a nicety, so that there isn't too much code between the tray and catch in main.
If you want, catch other exception types.
I quite like catching string error types, like std::string or char*, and printing those
in the catch handler in main.
Using exceptions like this at least allows RAII destructors to be called, so that they can do cleanup. Which can be pleasant and useful.
Overall, C error handling - exit and signals - and C++ error handling - try/catch/throw exceptions - play together inconsistently at best.
Then, where you detect an error
throw "error message"
or some more specific exception type.
Dude... exit()
function is defined under stdlib.h
So you need to add a preprocessor.
Put include stdlib.h
in the header section
Then use exit();
wherever you like but remember to put an interger number in the parenthesis of exit.
for example:
exit(0);
Either return a value from your main
or use the exit
function. Both take an int. It doesn't really matter what value you return unless you have an external process watching for the return value.
If you have an error somewhere deep in the code, then either throw an exception or set the error code. It's always better to throw an exception instead of setting error codes.
As Martin York mentioned, exit doesn't perform necessary clean-up like return does.
It's always better to use return in the place of exit. In case if you are not in main, wherever you would like to exit the program, return to main first.
Consider the below example. With the following program, a file will be created with the content mentioned. But if return is commented & uncommented exit(0), the compiler doesn't assure you that the file will have the required text.
int main()
{
ofstream os("out.txt");
os << "Hello, Can you see me!\n";
return(0);
//exit(0);
}
Not just this, Having multiple exit points in a program will make debugging harder. Use exit only when it can be justified.
If your if statement is in Loop You can use
break;
If you want to escape some code & continue to loop Use :
continue;
If your if statement not in Loop You can use :
return 0;
Or
exit();
Generally you would use the exit()
method with an appropriate exit status.
Zero would mean a successful run. A non-zero status indicates some sort of problem has occurred. This exit code is used by parent processes (e.g. shell scripts) to determine if a process has run successfully.
To break a condition use the return(0);
So, in your case it would be:
if(x==1)
{
return 0;
}
Source: Stackoverflow.com