isdigit() is for checking if a character is a digit character.
If you named it as isdigit('9') , it will return a nonzero value.
In the ASCII character set (which you most likely use), 9 is a horizontal tab that is not a digit.
Since you use input / output streams for input, you do not need to use isdigit() to validate the input. The extraction (i.e. std::cin >> k ) will fail if the data read from the stream is invalid, so if you expect to read the int and the user enters "asdf", the extraction will fail.
If the extraction fails, the error bit in the stream will be set. You can check this and handle the error:
std::cin >> k; if (std::cin) {
Note that the extraction itself also returns a stream, so you can simply shorten the first two lines:
if (std::cin >> k)
and the result will be the same.
James McNellis
source share