Just a guess, but I noticed that you scan an unsigned int and then it gets coerced into an int. Try keeping it an int all the way through.