delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/10/20/17:31:29

From: br5an AT aol DOT com (Br5an)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Scanf function
Date: 20 Oct 1997 18:01:27 GMT
Lines: 50
Message-ID: <19971020180100.OAA25465@ladder01.news.aol.com>
NNTP-Posting-Host: ladder01.news.aol.com
Organization: AOL http://www.aol.com
References: <62fp85$nq4$1 AT power DOT ci DOT uv DOT es>
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

martsobm AT uv DOT es (Marsel) wrote:

> I have writen this code

>#include <stdio.h>
>main()
>{
> char c;
> int n;
> printf("Write an integer: ");
> scanf("%d",&n);
> printf("Write a character: ");
> scanf("%c",&c);
> printf ("The integer is: %d and the character is: %d \n",n,c);
> }

>Why the secong scanf function does not wait until the caracter is introduced 
>?. the c variable always contain the caracter \010
> I know it will be better to use c=getch(); but why the this code does not 
>work well ?

   As per previous responce scanf can leave some unwanted codes in the input
 buffer. (010 is a line-feed). Also, some in the newsgroup are bound flame you
 about the return type of main( ) and the buffered output of printf ect... 
 (see djgppfaq.txt 9.4 ) should the occasion ever arise.  That's why I used
 conio.h functions. Here's some code for you to check out:

#include <conio.h>
int main()
{
 char c;
 int n;
 cprintf("Write an integer: ");
 cscanf("%d",&n);
 while (kbhit())
   getch();
 cprintf("\n\rWrite a character: ");
 cscanf("%c",&c);
 while (kbhit())
   getch();
 cprintf ("\n\rThe integer is: %d and the character is: %c \n",n,c);
 return(0);
}

  I'm not saying it's the perfect answer but it has worked for me. \n\r is to
 manually do your own line-feed and carriage-return.  Please note that kbhit()
 and getch() are not ANSI but are supported under DJGPP.  Hope that helps.

          Sincerely,
                    Br5an AT aol DOT com

- Raw text -


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