The root cause of your problem is that your code uses recursion instead of iteration.
main
makes a call to action
, which goes into a switch
, which then calls action
again, which goes into the switch
and calls action
, until the Quit option is selected.
At this point the recursive call chain starts to unwind. Unfortunately, since your code lacks break
statements, the switch
is not exited immediately. Instead, the code falls through to the next case
label, making you think that the return
did not do its job at terminating the action
. It did, but only the last action on the call stack is terminated. The remaining ones are still in progress, so they would continue as soon as the higher-level action finishes.
You can add break
statements to your switch
statement to mask the problem. However, the root cause would not go away: your program would remain poorly organized, and hard to read.
Consider rewriting the code using a while
loop in the action
function. Keep the switch
, add break
s, and remove recursive calls to action
from inside the switch. Instead, let the loop continue, so that the switch
is re-entered and processed again, until the Quit option is selected.