I got a function that assign a to 1 if it is 0, but when I call it again, a is already 1. I thought once it exits the function, all the variables in it would reset, isn't that how it is for other languages? What is going on?
I thought once it exits the function, all the variables in it would reset
No. In C++, local variables are are not initialized unless their type has a constructor. So every time you enter hi(), the variable a has an undefined value.
Put another way, local variables are not normally preserved between calls to the function. If you want to preserve the value, you must add static before the declaration (as lastchance mentioned)