delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/07/11/05:01:05

From: "Roger W. Huggins" <rogerwh AT aa DOT net>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Buffered input
Date: Sat, 11 Jul 1998 02:13:02 -0700
Organization: Home
Lines: 72
Message-ID: <6o79as$913$1@slave1.aa.net>
References: <6_Ap1.22$s7 DOT 1149262 AT alpha DOT sky DOT net>
NNTP-Posting-Host: 206.125.87.45
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

David, you are quite right.  There is a bug in DJGPP as compared to
Borland's scanf function.  I don't really know how to cure the problem
except for not using scanf.  I personally do not use scanf as it is too
limiting .  I would suggest using getch or getche depending on whether  or
not you want the entered chartered echoed to the screen.
Below is a modified version of your program which should work for you.

/* 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:\n ");
    c = getche();
    if(c == 'Y' || c == 'y' || c == 'N' || c == 'n')
      intScanres = 1;

    printf("intScanres = %d\n", intScanres);


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

David P. Hack wrote in message <6_Ap1.22$s7 DOT 1149262 AT alpha DOT sky DOT net>...
>I am a green C programmer and have a question about djgpp's handling of
>input and the scanf() function.
>
>The following program goes into a loop if a correct input is not received.
>What I expected it to do was to reprompt if a correct input is not
received.
>When I compile this source and run it under Turbo C, it reprompts but under
>djgpp it loops.  I assume this has something to do with djgpp's use of
>buffered input and I have read some of the bug descriptions that seem to be
>related to this but I am confused.  Can anyone help me understand why this
>loops and why it runs differently from Turbo C?
>
>Thanks
>Dave Hack
>
>/* shel06.c */
>/* Validate a single character input with scanf */
>#include <stdio.h>
>#include <conio.h>
>main()
>{
>  char c;               /* input character */
>  int intScanres;       /* scanf function return value */
>  clrscr();
>  do
>  {
>    fflush(stdin);
>    printf("Please input (Y)es or (N)o: ");
>    intScanres = scanf("%[YyNn]", &c);
>    printf("intScanres is %d\n",intScanres);
>  } while(intScanres != 1);
>  printf("The character entered is: %c",c);
>  return 0;     /* end of main() function */
>}
>
>
>


- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019