What you needed was a function that would terminate the program no matter how deep in the call stack it was, right? Just make a call to exit() where you need to terminate.
lol not really i just need a function that works if i press a button :D... ex:
i pressed ESC anytime in the program it should close
it doesnt work i tryed many functions
Well, that's something completely different. You never mentioned that, before.
There's nothing in the standard library that will allow you to do that. To do it, you'll need threads and some system-dependent function that will tell you which key's are being pressed. I think the function in Windows was GetAsyncKeyState().