www.delorie.com/djgpp/bugs/show.cgi   search  
Bug 000237

When Created: 07/11/1998 05:54:04
Against DJGPP version: 2.01
By whom: rogerh40@aa.net
Abstract: scanf does not work properly in a do while loop
When entering data using scanf looking for specific characters, if anything other than those specific characters are entered, the program starts looping continously and won't stop and ask for a new input.  Below is the program which 
is modified by me and is orginally from a question about this problem in the DJGPP newsgroup by David P. Hack. This program works fine with Borland C++ but not with DJGPP. I was wondering how to get this program working using scanf.  Getting it to work by using getch is quite simple, but I'm not really sure how to get scanf to work with this program.  It appears to be a bug in DJGPP, but I'm not sure.


/* shel06.c */
/* Validate a single character input with scanf */
#include <stdio.h>
#include <conio.h>
main()
{
  char c;               /* input character */
  int intScanres=0;       /* scanf function return value */
  clrscr();
  do
  {
    fflush(stdin);
    printf("Please input (Y)es or (N)o: ");
    scanf("%[YyNn]", &c);
    printf("c= %c\n",c);

    if(c == 'Y' || c == 'y' )
      {
          //do whatever for yes
          break;
      }
    else if(c == 'N' || c == 'n')
      {
         //do whatever for no
         break;
      }
    else if( c != 'Y' || c != 'y' || c != 'N' || c != 'n')
      {
            fflush(stdin);
    		printf("Please input (Y)es or (N)o: ");
    		scanf("%[YyNn]", &c);
    		printf("c= %c\n",c);

      }

  } while(intScanres != 1);
  printf("The character entered is: %c",c);
  return 0;     /* end of main() function */
}

Note added: 07/13/1998 13:19:32
By whom: broeker@physik.rwth-aachen.de
scanf() is *not* the problem here. fflush(stdin) is.
According to every standard on the C language I've seen, 
fflush(stdin) is *undefined*. Borland seems to have decided
to use this as a synonym for 'clear the keyboard buffer up
to the next line end'. DJGPP doesn't do that, which breaks this
buggy program.

To avoid such problems, just be sure you *empty* the input queue
after each call to scanf(), manually, or write a scanf() format string
that does so on its own:

	scanf("%[yYnN]%*s", &character)

or similar should do that (the '%*s' might have to be replaced by
some more sophisticated format, like '%*[-\n]\n' or whatever.

Note added: 04/19/1999 06:00:48
By whom: eliz@is.elta.co.il
I'm not sure fflush(stdin) is the cause here.  The test program has
a bug: it uses this:

     char c;
     ....
     scanf ("%[YyNn]", &c);

However, "%[]" does NOT read characters: it reads a string, and terminates
the characters it copies to the destination with a null character.  Therefore,
in the test program, it happily overwrites the stack (in practice, the
automatic variable which is allocated before c on the stack), which wreaks
havoc on the program.

I'm closing this bug.

Closed on 04/19/1999 06:00:50: Programmer's error, not a bug.
By whom: eliz@is.elta.co.il



  webmaster   donations   bookstore     delorie software   privacy  
  Copyright 2010   by DJ Delorie     Updated Jul 2010