unary - operator does exactly that. And for the if(a):
if(cin.good()) {}
checks if cin was able to read a number.