a scanf() question

M

Mik0b0

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!
 
R

Richard Bos

Mik0b0 said:
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!

You don't. You read numbers from the input (or, if you're wise, you read
strings, and then attempt to convert those to numbers - much less prone
to breakage when someone enters "three" rather than "3"); and _then_ you
check whether the number you read is within the desired limits. There is
no single function in C that can read a number only if it's larger or
smaller than a certain limit.

Richard
 
R

Richard Heathfield

Richard Bos said:
You don't. You read numbers from the input (or, if you're wise, you read
strings, and then attempt to convert those to numbers - much less prone
to breakage when someone enters "three" rather than "3"); and _then_ you
check whether the number you read is within the desired limits. There is
no single function in C that can read a number only if it's larger or
smaller than a certain limit.

By "no single function" Richard means "no single standard library function",
and by "a certain limit" he means "a certain user-imposed limit".

The second nit refers to the fact that scanf can only read a number that is
within the range of values for the given type, and the first refers to the
fact that any competent C programmer can write a C function to do what is
required and so it is entirely possible - even likely - that someone
already has.
 
S

Simon Biber

Mik0b0 said:
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.
 
M

Mik0b0

Thanks to all who replied! Special thanks to Simon: the trick with n <
10 && scanf("%d", &i) == 1 is great. Have a nice day ;)
 
M

mark_bluemel

Keith said:
What exactly do you find so amusing?

Perhaps I misunderstood Simon's intent, but I thought his code which
verged on obfuscation (IMHO), did no error handling and threw away the
data it found was quite amusing. The fact that the original poster
seemed to like it amused me even more.

You may have a different point of view.

Here, for reference, is Simon's main loop:-

for(n = 0;
n < 10 && scanf("%d", &i) == 1;
n += -399 <= i && i <= 400);
 
S

Simon Biber

Perhaps I misunderstood Simon's intent, but I thought his code which
verged on obfuscation (IMHO), did no error handling and threw away the
data it found was quite amusing. The fact that the original poster
seemed to like it amused me even more.

You may have a different point of view.

Here, for reference, is Simon's main loop:-

for(n = 0;
n < 10 && scanf("%d", &i) == 1;
n += -399 <= i && i <= 400);

Yes, it is mildly obfuscated. It's not that difficult to follow though:
a loop on the variable n usually takes the form:
for(n = 0; n < maximum; n += increment);

I have added another condition: that the return value of scanf is one.

The increment is a little complex, since the value is the boolean result
of whether the read value i is in the correct range: 0 for no (in which
case n is unchanged) and 1 for yes (in which case n is incremented).

There _is_ error handling. The program checks the return value of scanf.
If a matching error occurs, then the loop is ended without going on to
the 'increment' portion. So it avoids reading the invalid/uninitialised
value of i.

If you wanted to do something with the read values, such as printing
them, storing them, passing them to a function, etc., all you need do is
add that code to the, currently empty, body of the loop.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,526
Members
44,997
Latest member
mileyka

Latest Threads

Top