0

I am trying to get input from the user/ file redirection and need to return an error when the input is not an integer. For this, I am using

int s= scanf("%d", x);
if (s==0){
    fprintf(stderr, "Non integer value input.");
}

but this means that I can not use it to terminate the program when I run out of inputs. How can I accomplish both since getting the number of successful scans is the only way I know to figure out if the input has ended.

  • You could use `fgets()` to read lines and `sscanf` to do formatted extraction from those lines. You also have `feof(stdin)`. – Ted Lyngmo Sep 12 '21 at 01:38
  • `scanf`'s error handling is one step removed from useless. So after reading just the words "Checking that the input is" from your question title, I'm already thinking that the answer is "don't use `scanf`". Seriously. If you're writing a toy problem where errors don't matter, `scanf` can be okay. For anything else, you want to [use something other than `scanf`](https://stackoverflow.com/questions/58403537). – Steve Summit Sep 12 '21 at 01:45

0 Answers0