I currently have a program which has the following basic structure
main function
— displays menu options to user
— validates user input by passing it to a second function (input_validator)
— if user selects option 1, run function 1, etc
function1,2,3,etc
— input is requested from user and then validated by input_validator
— if input_validator returns true, we know input is good
Here is my problem. I want to allow the user to quit at any point within the program by typing ‘0’. I planned on doing this with some basic code in input_validator (if input = 0, etc).
This would appear to be simple, but I have been told that using quit() will result in some resources never been released / etc. I cannot simply do a ‘break’ either — it will result in my program simply returning to the main function.
Any ideas?
One possibility would be to do it by throwing an exception that you catch in main, and when you catch it, you exit the program. The good point of throwing an exception is that it lets destructors run to clean up objects that have been created, which won’t happen if you exit directly from elsewhere (e.g., by using
exit()).