M
mdbenchrifa
(e-mail address removed) a écrit :
Just add gets(str) like
int main()
{
int n;
char str[10];
while (1)
{
printf("Please enter positive number \n");
scanf("%d",&n);
if ( n<=0 )
{
printf("Illegal input!\n");
gets(str);
continue;
}
return 0;
}
system("PAUSE");
return 1;
}
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; }
Just add gets(str) like
int main()
{
int n;
char str[10];
while (1)
{
printf("Please enter positive number \n");
scanf("%d",&n);
if ( n<=0 )
{
printf("Illegal input!\n");
gets(str);
continue;
}
return 0;
}
system("PAUSE");
return 1;
}