|
From: Ioannis V. <no...@ho...> - 2002-08-08 08:27:39
|
> -----Original Message-----
> From: dev...@li...
> [mailto:dev...@li...] On Behalf
> Of Daniel K. O.
> Sent: Thursday, August 08, 2002 2:34 AM
> To: Dev-Cpp-Users
> Subject: Re: [Dev-C++] compiler psychiatrics
>
> When scanf() can't find the non white space caracter, it
> normally finish,
> and some caracters can remain in the keyboard buffer, so it
> will mess with
> another scanf() calls if you don't use fflush(stdin) before them.
Although widely used, fflush(stdin) has undefined behaviour. fflush() is
made to work with output streams, not input. So it may crash your PC or
cause some acccident. You do not want that, do you?
So if you want to flush stdin you have to use something like that:
void stdinflush(void)
{
while(getchar()!='\n')
;
}
A working example:
#include <stdio.h>
void stdinflush(void)
{
while(getchar()!='\n')
;
}
int main(void)
{
char c;
scanf("%c", &c);
stdinflush();
printf("\nYou entered: %c\n", c);
return 0;
}
Another thing is that scanf() is dangerous and should be avoided if
possible.
Ioannis
* Ioannis Vranos
* Programming pages: http://www.noicys.cjb.net
* Alternative URL: http://run.to/noicys
|