Is there any command I can use to stop my program? I would usually just stick a return 0; in but this time I want it to stop the program from within another function besides main.
This is a discussion on Stopping a program within the C Programming forums, part of the General Programming Boards category; Is there any command I can use to stop my program? I would usually just stick a return 0; in ...
Is there any command I can use to stop my program? I would usually just stick a return 0; in but this time I want it to stop the program from within another function besides main.
Look up the exit() function, declared in <stdlib.h>.
Right 98% of the time, and don't care about the other 3%.
I'm not allowed to do that, so I guess I'm going to have to find another approach to my problem. I'll be back if I get stuck again.
I assume, if you are not allowed to use exit(), you are also not allowed to use abort() or assert().
The only other option - at least, within bounds of standard C - is to all design your functions so, when you need to exit, all called functions back out until control returns to main().
Right 98% of the time, and don't care about the other 3%.
You could also continue to return 0 (as an example) from your function to indicate an error, then handle it in the function making the call by checking the return value. Like alot of functions in the standard library does.