The code you ask about causes getint
to stop if the next character in the stream is not part of a numeral (or a space) because it is not a digit or sign character.
The notion is that as long as you call getint
while there are acceptable numerals in the input, it will convert those numerals to int
values and return them. When you call getint
while there is not an acceptable numeral next in the input, it will not perform a conversion. Since it is not performing a character, it leaves the character it is not using in the stream.
A proper routine would return an error indication so the caller can easily determine that getint
is not performing a conversion for this reason. As it is, the caller cannot distinguish between getint
returning 0 for a “0” in the input and getint
returning 0 for a non-numeral in the input. However, as this is only tutorial code, features like this are omitted.