getint(): An Inspiration

Ruoyu Zhong

While doing my C/C++ assignment, I suddenly felt like creating a C/C++ function which can read integers from the input buffer, and has a good management of invalid input. Then I created the function getint() using getchar().

It has the following features:

  • It can omit all the whitespaces before and after the integer.
  • It does a cleanup to prevent malfunction due to invalid input.
  • It can provide error information through a pointer parameter. (In C++, it is NULL by default; in C, if no error information is needed, a NULL pointer needs to be provided.)

After creating it, I have been told that there are quite a number of similar functions existing already. But still I decided to put it here. It is also available in GitHub. When you find bugs or areas of improvement, please feel free to point them out by creating a new issue.