Mail Archives: djgpp/1997/10/20/17:31:29
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 -