Re: scanf behaviour
- From: Ian Collins <ian-news@xxxxxxxxxxx>
- Date: Sat, 25 Nov 2006 11:56:08 +1300
stasgold@xxxxxxxxx wrote:
Hello.That's because the conversion fails and the characters remain in the
I maybe reinvent the weel ...
I'm trying to read positive integer number with the help of scanf, if
the input value is not positive number but negaive one zero or char , i
have to reread the input until I get the needed pos. number
I wrote the code , but it doesn't work the way it should : if i put
some char into input, the program goes infinite loop instead of
promting me to enter a new value.
stream. So the next time scanf is called, the same character sequence is
read.
Add
fflush( stdin );
after your printf.
Please shed some light on what is the problem. thanks
[code]
#include <stdio.h>
int main()
{
int n;
while (1)
{
printf("Please enter positive number \n");
if ( scanf("%d",&n)!=1 || n<=0 )
{
printf("Illegal input!\n");
continue;
}
return 0;
}
return 1;
}
[/code]
--
Ian Collins.
.
- Follow-Ups:
- Re: scanf behaviour
- From: Richard Heathfield
- Re: scanf behaviour
- References:
- scanf behaviour
- From: stasgold
- scanf behaviour
- Prev by Date: Re: Question regarding fgets and new lines
- Next by Date: Re: screen clearing in ANSI C
- Previous by thread: scanf behaviour
- Next by thread: Re: scanf behaviour
- Index(es):
Relevant Pages
|
|