but gets in an endless loop with alphabetical input. Why?
Because the insertion operator>> knows that a number can't contain a non-digit value and if it encounters a non-digit value it places the stream into a failure state. When the stream is in a failure state no further processing will occur with that stream until the error flags are cleared and the offending data is removed from the stream.
if you want to handle the errors, you can read everything as a string, then check to see if the string is a valid value and if so, convert & move it to the target, if not, request a new value until it passes.
different compilers may handle this differently. Mine just assigns zero to entry when junk is put in.
I modified my code in two ways. First of all converting string to number (http://www.cplusplus.com/forum/articles/6046/) Then in another solution I used Handy Andy's hint. Both are much better than my original one (what else could be :-) but neighter of them are perfect.
When user enters a junk input - leading number - (i.e 9asd) the codes handles inputs like this as a valid entry (i.e. 9)
Why, and what modifications needs to get rid of this phenomenon?
why is because it is valid .. stoi etc type functions can pull out a partial valid response of 9 and ignore the bad stuff. Sometimes that is useful, sometimes not. You can read online help / manuals on functions like that to see what they do with various input, such as http://en.cppreference.com/w/cpp/string/basic_string/stol
a really lazy way to do what you want is a double conversion.
1) convert string to int
2) convert that int back to a string
3) is result of 2 == to original input? if yes, valid, else not... you may need to trim whitespace or something for this to work.