Re: a scanf() question
- From: "Mik0b0" <newssw@xxxxxxxxx>
- Date: 28 Nov 2006 09:42:33 -0800
Thanks to all who replied! Special thanks to Simon: the trick with n <
10 && scanf("%d", &i) == 1 is great. Have a nice day ;)
Simon Biber wrote:
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.
.
- References:
- a scanf() question
- From: Mik0b0
- Re: a scanf() question
- From: Simon Biber
- a scanf() question
- Prev by Date: Re: Reading a string of unknown size
- Next by Date: Re: Code fails with Segmentation Fault
- Previous by thread: Re: a scanf() question
- Next by thread: Code fails with Segmentation Fault
- Index(es):
Relevant Pages
|
|