Accept Numerical Values only for input using scanf

*爱你&永不变心* 提交于 2019-11-30 23:31:53

If you really want to deal with user input that could be hostile use a separate function for getting the number.

Allows
- leading spaces : " 123"
- trailing spaces : "123 "
- leading zeros : "0000000000000000000000000000000000123"
- Rescans nicely after error input.
Catches the following errors
- No input : ""
- Extra text after the number: "123 abc"
- Text before the number : "abc 123"
- Split number : "123 456"
- Overflow/underflow : "12345678901234567890"
- other : "--123"
Re-prompts on invalid input.

#include <errno.h>
#include <stdio.h>
#include <stddef.h>

int GetInteger(const char *prompt, int *i) {
  int Invalid = 0;
  int EndIndex;
  char buffer[100];
  do {
    if (Invalid)
      fputs("Invalid input, try again.\n", stdout);
    Invalid = 1;
    fputs(prompt, stdout);
    if (NULL == fgets(buffer, sizeof(buffer), stdin))
      return 1;
    errno = 0;
  } while ((1 != sscanf(buffer, "%d %n", i, &EndIndex)) || buffer[EndIndex] || errno);
  return 0;
}

int main() {
  int a, b, c;
  if (GetInteger("Enter first number to add\n", &a)) {
    ; // End of file or I/O error (rare)
  }
  if (GetInteger("Enter second number to add\n", &b)) {
    ; // End of file or I/O error (rare)
  }
  c = a + b;
  printf("Sum of entered numbers = %d\n",c);
  return 0;
}

BTW, You should not do printf("Enter first number to add\n"). Use fputs() instead. Consider what would happen if the string had a % in it.

Please read the manual page for scanf. You need to check the return value. SHould return 1 if able to match a number.

It's better to avoid using scanf at all. Use fgets to get entire line, and then use sscanf to extract the information you need. Check the return value of sscanf to make sure the input is as expected.

EDIT- I needed to add the getchar() in the while loop because the unread non-numeric item was left in the input queue causing the program to go in an infinite loop , further I have also added more compact form of while loop for the same thing , both will have the same effect .

You can check the return value of scanf it returns 1 on successful match with the format specifier and 0 if not . You can do it like this for your program :

#include <stdio.h>
#include <stdbool.h>
int main(void)
{ 
  int num;
  bool status;//_bool may also be used and stdbool.h won't be needed for that

  status = scanf ("%d",&num);

  if (status != 1 )
    {
      getchar();// needed to add it to eat up the unread item left in input queue.
      while (status != 1)
        {
          printf (" sorry ! incorrect input :ERROR: try again");

          status = scanf("%d", &num);

        }
    }
  /*....do something...*/
  return 0;
}

More compact form of while loop :-

while (scanf("%d",&num) != 1)
    {
      getchar();
          printf (" sorry ! incorrect input :ERROR: try again\n");
    }

Besides, you should always use meaningful names for variables like num1 and num2 for instance instead of a and b .

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!