Keep NumLock always on in Linux
I don't know much C++, but your indentation isn't great. To fix this you could try running your code through a C/C++ linter.

Your code is also having a much larger performance hit than if you told your Xorg to do this. Assuming that you only want this to work in Xorg, rather than in both the console and Xorg, you can use setxkbmap as described in the Arch wiki page Keyboard configuration in Xorg. setxkbmap comes with a couple of options to change how your keyboard works, listed under /usr/share/X11/xkb/rules/base.lst. From this list numpad:mac looks like what you want, and will change your keyboard to always use numbers whether numlock is on or not.
And so I would use:

setxkbmap -option numpad:mac