Well, first of all, you are using global variables. I mean no disrespect but you really should go back and learn C++ (simple terminal stuff). Once you have a variable outside the current scope it won't reset until you tell it too.
And also, DO NOT use global variables... never...