The request is that the user must input numbers until they fit within the numbers 1 and 13
for (N=0; N>13 && N<1; scanf("%d",&N))
{
printf("fits");
}
This doesn't work, do I have to rephrase it somehow?
The request is that the user must input numbers until they fit within the numbers 1 and 13
for (N=0; N>13 && N<1; scanf("%d",&N))
{
printf("fits");
}
This doesn't work, do I have to rephrase it somehow?
Your programmed condition N>13 && N<1
can never be true. So your loop body and the scanf("%d",&N)
is never executed.
You do not want to "loop while the number is bigger than 13 AND lower than 1": you probably want to "loop while the number bigger than 13 OR lower than 1".
So N > 13 || N < 1
.
N > 13 && N < 1
will always be
false.A number can never be greater than 13 and less than 1 at the same time
|| operator
for the comparison like N > 13 || N < 1
, Also N = 0
is redundant in your code.fits
every time it is not in the range.int main()
{
int input;
int flag = scanf("%d", &input) == 0 ? 1 : 0;
while (input < 1 || input > 13)
{
if (flag)
{
while (getchar()!='\n');
}
int flag = scanf("%d", &input) == 0 ? 1 : 0;
}
printf("fits");
return 0;
}
while (getchar()!='\n')
is used to clean the stdin
.
when a user enters a non-numeric character
scanf
returns 0, So to prevent unexpected behavior you've to cleanstdin
first before calling anotherscanf
statement.