0

As read in this answer:

num will always contain an integer because it's an int. The real problem with your code is that you don't check the scanf return value. scanf returns the number of successfully read items, so in this case it must return 1 for valid values. If not, an invalid integer value was entered and the num variable did probably not get changed (i.e. still has an arbitrary value because you didn't initialize it).

As of your comment, you only want to allow the user to enter an integer followed by the enter key. Unfortunately, this can't be simply achieved by scanf("%d\n"), but here's a trick to do it:

  int num;
  char term;
  if(scanf("%d%c", &num, &term) != 2 || term != '\n')
      printf("failure\n");
  else
      printf("valid integer followed by enter key");

I saw this post works perfectly, but it does not work inside a do-while loop. I need to get from user until the correct input but when the loop is executed first, it so on continuously printing the failure without getting from user.

What is the problem?

Paul R
  • 208,748
  • 37
  • 389
  • 560
Raj pradeep
  • 37
  • 2
  • 6
  • 3
    *does not works in do-while loop* - so where's the code that doesn't work? How are we to know what you did badly if you don't post it? Make a self-contained, small, buildable example using this in a while loop and showing the specific problem you have, with expected and present behavior. – Bartek Banachewicz Jan 07 '19 at 11:06
  • 1
    please add your code, so that we can help – moghya Jan 07 '19 at 11:15
  • `int myInt; scanf("%d", &myInt);` should take care about integers only, but If I can remember then you should flush input buffer - `fflush` function maybe, I am not sure (otherwise enter key will be in buffer) – xxxvodnikxxx Jan 07 '19 at 11:26
  • May I ask why you insist that the newline character be present? In my experience terminal input is rare in real-world applications. They occur only in interactive programs, most likely in shells and editors. The much more common use case is a program which reads from its standard input which may as well be redirected data from a program or file. In that case the input may or may not be terminated by a newline; but this is likely irrelevant. If you are indeed working on an interactive program I'd suggest you strictly separate the user interaction from processing. – Peter - Reinstate Monica Jan 07 '19 at 11:46

1 Answers1

1

The problem is that the offending characters are not cleared from the input buffer. If you want to clear until the first end of line after an error, you have to do it explicitely:

int num;
char term;
for (;;) {
  if(scanf("%d%c", &num, &term) != 2 || term != '\n') {
      printf("failure\n");
      int c;
      while (('\n' != (c=fgetc(stdin))) && (c != EOF));   // clear up to end of line
      if (c == EOF) break;     // do not read past EOF...
  }
  else { 
      printf("valid integer followed by enter key");
      break;
  }
}
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252