-2
#include<stdio.h>
int main()
{
  int n;
  printf("write any integer:\n");
  scanf("%d",&n);
}

I want to show error if input data by user will other than integer type. Then what may the conditions by which we can decide that entered data is integer type ?

if I write a character (let 'a') insted of integer type then how to identify that entered data is not integer and then show error that "You haven't entered integer".

I think It can be possible by using return type of scanf() or typecasting but I am not sure that it will work or not and how it will work?

Suraj
  • 91
  • 1
  • 7
  • 4
    Checking the return value of `scanf()` cannot detect some floating-point numbers like `3.14`. You should read the input via `fgets()` and parse the string read to check. `strtol()` is useful for parsing integers. – MikeCAT Jun 28 '21 at 16:23
  • 1
    `scanf` is really, really bad at handling incorrect input. Using `%d` says you want to read an integer -- but it gives you virtually no control over what happens if the user types something other than a valid integer. – Steve Summit Jun 28 '21 at 16:34
  • 1
    See [this question](https://stackoverflow.com/questions/58403537) for alternatives to using `scanf` (although the answers at that question don't talk specifically about handling incorrect input). – Steve Summit Jun 28 '21 at 16:34
  • 1
    Handling bad input is an important but surprisingly hard problem. First you might want to think about which, if any, of these inputs you want to count as wrong: `1`, `-1`, `01`, `000001`, `1.`, `1.1`, `1x`, `1e`, `1e1`, `100000000000000000`, `1000000000000000000000000000`. You might also have to think about (a) leading/trailing whitespace, and (b) leading/trailing `\n`. – Steve Summit Jun 28 '21 at 16:37
  • @MikeCAT: Ok Thanks, I will try strtol() function of string. – Suraj Jun 28 '21 at 16:38
  • @SteveSummit: It means scanf() don't have any control on incorrect input. and we can't use it to know about the incorrent input. – Suraj Jun 28 '21 at 16:41
  • @SteveSummit: I got the point from the qustion link you have provided. It's good to use fgets() to take input and convert the input in to integer or floating point as per need by using functions of string. It's also suggested by MikeCAT. Thanks a lot. – Suraj Jun 28 '21 at 16:48
  • @SteveSummit: Of course there are lots of possibilities and it's tough to handling incorrect inputs. – Suraj Jun 28 '21 at 16:51

1 Answers1

0

I just wrote this code a few days ago for determining if the user input is an integer or not. You'll have to change the size of buf in case the input gets too large or buf will overflow. Also as Steve Summit said,

Handling bad input is an important but surprisingly hard problem. First you might want to think about which, if any, of these inputs you want to count as wrong: 1, -1, 01, 000001, 1., 1.1, 1x, 1e, 1e1, 100000000000000000, 1000000000000000000000000000. You might also have to think about (a) leading/trailing whitespace, and (b) leading/trailing \n

In case of the code below, inputs like 1 1, 1.1, .1, 1e1, 1x, a are considered Not Int while inputs like 100, 100 , 100, -10 are considered Int

#include<stdio.h>
#include<ctype.h> //using ctype.h as suggested by Steve Summit
int check(char *buf){ //for checking trailing whitespace characters
  for(int i=0;buf[i]!='\0';i++){
   if(!isspace(buf[i])){
      return 0;
    }
  }
  return 1;
}
int main(){
int x;
char buf[10]; //change buf to appropriate length based on the input or it may overflow
scanf("%d",&x);
if(scanf("%[^\n]",buf)==0||check(buf)){
  printf("Int");
}
else{
  printf("Not Int");
}
}