scanf behaviour
- From: stasgold@xxxxxxxxx
- Date: 24 Nov 2006 14:32:20 -0800
Hello.
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.
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]
.
- Follow-Ups:
- Re: scanf behaviour
- From: mdbenchrifa
- Re: scanf behaviour
- From: Jens Thoms Toerring
- Re: scanf behaviour
- From: Ian Collins
- Re: scanf behaviour
- Prev by Date: Re: dyld_stub_binding_helper undefined symbol / crt1.o link error
- Next by Date: screen clearing in ANSI C
- Previous by thread: Most Interesting Bug Track Down
- Next by thread: Re: scanf behaviour
- Index(es):
Relevant Pages
|
|