Re: a scanf() question



Mik0b0 wrote:
Good day to everyone,
I have trouble finding a solution to this problem: how to pick up 10
numbers
corresponding to certain criteria (-399>=number<=400) from the input?
The number of input numbers is not limited. Thanks!

#include <stdio.h>

int main(void)
{
int i,n;
for(n = 0;
n < 10 && scanf("%d", &i) == 1;
n += -399 <= i && i <= 400);
return 0;
}

Here's an example run:

C:\docs\prog\c>a
100
200
300
400
500
600
700
800
100
200
300
400
500
600
700
800
100
200

It ignored the 500, 600, 700 and 800 in the input, and continued taking input until it had read 10 numbers in the correct range. Those were 100, 200, 300, 400, 100, 200, 300, 400, 100, 200.

--
Simon.
.



Relevant Pages