Code only works correctly the first time?

Sorry, there were some errors with the way I posted the code. It should compile now.