473,322 Members | 1,806 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

a scanf() question


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!

Nov 28 '06 #1
8 1417
"Mik0b0" <ne****@gmail.comwrote:
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
Nov 28 '06 #2
Richard Bos said:
"Mik0b0" <ne****@gmail.comwrote:
>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.
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.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Nov 28 '06 #3
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.
Nov 28 '06 #4
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.
Nov 28 '06 #5

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

Nov 29 '06 #6
ma**********@pobox.com writes:
Mik0b0 wrote:
>Thanks to all who replied! Special thanks to Simon: the trick with n <
10 && scanf("%d", &i) == 1 is great. Have a nice day ;)

ROTFL...
What exactly do you find so amusing?

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 29 '06 #7

Keith Thompson wrote:
ma**********@pobox.com writes:
Mik0b0 wrote:
Thanks to all who replied! Special thanks to Simon: the trick with n <
10 && scanf("%d", &i) == 1 is great. Have a nice day ;)
ROTFL...

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);

Nov 29 '06 #8
ma**********@pobox.com wrote:
Keith Thompson wrote:
>ma**********@pobox.com writes:
>>Mik0b0 wrote:
Thanks to all who replied! Special thanks to Simon: the trick with n <
10 && scanf("%d", &i) == 1 is great. Have a nice day ;)
ROTFL...
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);
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.

--
Simon.
Nov 30 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Steve Zimmerman | last post by:
This post is not intended as an argument to any other post, just some simple scanf experiments that I wanted to share. I found experiments 5 and 6 the most educational. Also, I thought...
39
by: Teh Charleh | last post by:
OK I have 2 similar programmes, why does the first one work and the second does not? Basically the problem is that the program seems to ignore the gets call if it comes after a scanf call. Please...
11
by: | last post by:
Hi all...this is a great forum, In one of my posts, someone tell me that is more secure use input function with 'A field-width specifier'... So my question, which function i should use ?. ...
33
by: Lalatendu Das | last post by:
Dear friends, I am getting a problem in the code while interacting with a nested Do-while loop It is skipping a scanf () function which it should not. I have written the whole code below. Please...
7
by: sajjanharudit | last post by:
Can anyone explain me what is happening in the following code: #include<stdio.h> int main() { int i,j; scanf("%d %d" + scanf("%d %d",&i,&j)); printf("%d %d\n"); }
30
by: James Daughtry | last post by:
char array; scanf("%19s", &array); I know this is wrong because it's a type mismatch, where scanf expects a pointer to char and gets a pointer to an array of 20 char. I know that question 6.12...
14
by: main() | last post by:
I know this is the problem that most newbies get into. #include<stdio.h> int main(void) { char a; scanf("%c",&a); /*1st scanf */ printf("%c\n",a); scanf("%c",&a); /*2nd scanf*/...
1
by: nozone | last post by:
Hi all. I am beginner in C. My question is: What is " \n" inside scanf()? Here is part of the implementation of an example: ... char myChar; int myID; scanf("%c", &myChar); scanf("%d\n";...
2
by: subramanian100in | last post by:
Consider the following program named as x.c #include <stdlib.h> #include <stdio.h> int main(void) { unsigned int u; char str;
3
by: yxxxxy | last post by:
Hi, this is a part of my program code. i want to ask two questions. int time; float rate; float salary; printf("Enter # of hours worked (-1 to end):"); scanf("%d",&time);
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.