Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > how to check the scanf function if it will read more than one number

Reply
Thread Tools

how to check the scanf function if it will read more than one number

 
 
Richard Heathfield
Guest
Posts: n/a
 
      01-02-2006
pemo said:

> I would suggest
> that, *if* your code need *not* be portable, and *if* your implementation
> of fflush works on stdin ok, well, you should perhaps stick with it then!


Bad advice. Better to avoid the problem completely, which is trivial.

> I'll get flamed for suggesting this of course - or - better still,
> someone will come up with a portable way to flush stdin


That doesn't make sense, since flushing is not something we do to input.
I've already posted a portable way to discard characters from stdin until a
delimiter is encountered.

I never, ever have to solve this problem in my own code, because I capture
all text input a line at a time. Why don't you just do that too?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
 
Reply With Quote
 
 
 
 
Flash Gordon
Guest
Posts: n/a
 
      01-02-2006
moosdau wrote:
> Keith Thompson wrote:
>> scanf() returns the number of items assigned. If you give it " 32 a ",
>> it will assign 32 to dend and fail to assign a value to dor; scanf()
>> will then return 1. If you want to assign both values, you need to
>> check whether scanf() returned 2.
>>
>> You should pick a better name than "temp1". The simplest thing to do

>
> sorry,because I copied it from a long code,
> if I named every variant with its meaning, there must be too many
> temporary variants.
> so ......


If that is a problem it sounds like your function is too long and it
should be split in to multiple functions.

>> is to assign the result of scan() to a variable, and test the value
>> of that variable. Pseudo-code follows:
>>
>> do {
>> printf("...\n");
>> items_read = scanf(...);
>> } while (items_read != 2);
>>
>> Don't use fflush(stdin). The fflush() function is defined only for
>> output streams.

>
> Thanks very much!!
> but I don't know very clearly why shouldn't I use the fflush function.
> if there is not any usable data in the input stream,
> could I use it to clear the input stream?
> if not , could you please tell me why?


<snip>

You should not use it in input streams because it is not defined. That
means anything can happen, from what you expect to your computer growing
arms and hands and punching you in the nose. More likely effects, if it
appears to work on your current system, are you doing an update and it
suddenly not doing what you expect but either doing nothing or giving
some kind of access violation error.

If you want to clear the input stream you have to decide on what you
mean, possibly reading the rest of the line, and do that. If you really
do want to throw away anything the user has typed so far (what if input
is piped in from a file though!) and get the next thing the user types,
then you can't do that in standard C and will have to use system
specific routines.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
 
Reply With Quote
 
 
 
 
pemo
Guest
Posts: n/a
 
      01-02-2006

"Richard Heathfield" <(E-Mail Removed)> wrote in message
news:dpb9pp$d92$(E-Mail Removed)-infra.bt.com...
> pemo said:
>
>> I would suggest
>> that, *if* your code need *not* be portable, and *if* your implementation
>> of fflush works on stdin ok, well, you should perhaps stick with it then!

>
> Bad advice. Better to avoid the problem completely, which is trivial.


But, I'd wager, not as trivial as scanf("%d", &n); fflush(stdin); if it were
fully supported? Aren't you talkig about something like this?

char buffer[100];

int n;

if(fgets(buffer, sizeof(buffer), stdin))
{
if(sscanf(buffer, "%d", &n))
{
printf("w00t, we read an int! (%d)\n", n);
}
}

>> I'll get flamed for suggesting this of course - or - better still,
>> someone will come up with a portable way to flush stdin

>
> That doesn't make sense, since flushing is not something we do to input.
> I've already posted a portable way to discard characters from stdin until
> a
> delimiter is encountered.


I know what you mean [the 'logic' of it], but as this scanf type question is
often asked here, it obviously does make sense in that context - e.g., since
scanf is provided, and as that function is invariably the function that's
used in tutorials, it *would* make sense [surely?] to have some way of
removing any unread characters held in the stdin stream?

> I never, ever have to solve this problem in my own code, because I capture
> all text input a line at a time. Why don't you just do that too?


Sure, it's a good way to handle the problem, but see my wager.



 
Reply With Quote
 
Chuck F.
Guest
Posts: n/a
 
      01-02-2006
pemo wrote:
>

.... snip ...
>
> I know what you mean [the 'logic' of it], but as this scanf type
> question is often asked here, it obviously does make sense in
> that context - e.g., since scanf is provided, and as that
> function is invariably the function that's used in tutorials, it


Not in good tutorials.

> *would* make sense [surely?] to have some way of removing any
> unread characters held in the stdin stream?


We do. My variant of Richards function is:

int flushln(FILE *f)
{
int ch;

do {
ch = getc(f);
} while (('\n' != ch) && (EOF != ch));
return ch;
}

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
 
Reply With Quote
 
pemo
Guest
Posts: n/a
 
      01-02-2006

"Chuck F. " <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> pemo wrote:
>>

> ... snip ...
>>
>> I know what you mean [the 'logic' of it], but as this scanf type
>> question is often asked here, it obviously does make sense in
>> that context - e.g., since scanf is provided, and as that
>> function is invariably the function that's used in tutorials, it

>
> Not in good tutorials.


I didn't think that there were any????

>> *would* make sense [surely?] to have some way of removing any
>> unread characters held in the stdin stream?

>
> We do. My variant of Richards function is:
>
> int flushln(FILE *f)
> {
> int ch;
>
> do {
> ch = getc(f);
> } while (('\n' != ch) && (EOF != ch));
> return ch;
> }


Would this be acceptable for stdin?

void flushstdin(void)
{
while(getchar() != '\n')
;

return;
}


 
Reply With Quote
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      01-02-2006
moosdau a écrit :
> do you mean the linux os?
> but I'm using windows,so I can't use the "man" command.
> could you tell me plz?


google 'man <item>' works too...

--
A+

Emmanuel Delahaye
 
Reply With Quote
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      01-02-2006
pemo a écrit :
> There's no easy way to purge stdin, esp when it's already potentially empty,
> i.e., most [all that I can think of right now] std functions that could
> potentially test for this will *wait* if stdin is empty, e.g., getchar,
> scanf, gets, fgets, feof, ... it's a toughie, and I would suggest that, *if*
> your code need *not* be portable, and *if* your implementation of fflush
> works on stdin ok, well, you should perhaps stick with it then! I'll get
> flamed for suggesting this of course - or - better still, someone will come
> up with a portable way to flush stdin


It's a design issue. Using the proper input function in the proper way
avoids these problems of pending characters. The canonic way :

char line[PROPER_SIZE];

if (fgets(line, sizeof line, stdin) != NULL)
{
/* search */
char *p = strchr(line, '\n');
if (p != NULL)
{
/* kill */
*p = 0;
}
else
{
/* purge */
int c;
while ((c = fgetc(stdin)) != '\n' && c != EOF)
{
}
}
}

stick this into some getline() function of your own, add the required
headers (<stdio.h> and <string.h>) and you have a decent input function
for life.

--
A+

Emmanuel Delahaye
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      01-02-2006
"pemo" <(E-Mail Removed)> writes:
> "Chuck F. " <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...

[...]
>> We do. My variant of Richards function is:
>>
>> int flushln(FILE *f)
>> {
>> int ch;
>>
>> do {
>> ch = getc(f);
>> } while (('\n' != ch) && (EOF != ch));
>> return ch;
>> }

>
> Would this be acceptable for stdin?
>
> void flushstdin(void)
> {
> while(getchar() != '\n')
> ;
>
> return;
> }


What if getchar() returns EOF before returning '\n'?

Note that there are two different things being discussed here. One,
which is easily implementable in standard C, is discarding all the
remaining characters in a line. The other, which can't be done in
standard C, is discarding all typed characters that haven't been
processed yet, i.e., flushing the typeahead buffer.

I think the former is what the OP really wants.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <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.
 
Reply With Quote
 
pemo
Guest
Posts: n/a
 
      01-02-2006

"Keith Thompson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "pemo" <(E-Mail Removed)> writes:
>> "Chuck F. " <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...

> [...]
>>> We do. My variant of Richards function is:
>>>
>>> int flushln(FILE *f)
>>> {
>>> int ch;
>>>
>>> do {
>>> ch = getc(f);
>>> } while (('\n' != ch) && (EOF != ch));
>>> return ch;
>>> }

>>
>> Would this be acceptable for stdin?
>>
>> void flushstdin(void)
>> {
>> while(getchar() != '\n')
>> ;
>>
>> return;
>> }

>
> What if getchar() returns EOF before returning '\n'?


Surely, if there's a \n in the buffer, getchar won't return EOF?

If there's not a \n in the buffer, I can see that testing for EOF might be a
good idea.


 
Reply With Quote
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      01-02-2006
pemo a écrit :
> Would this be acceptable for stdin?
>
> void flushstdin(void)
> {
> while(getchar() != '\n')
> ;
>
> return;
> }


No, because stdin can be redirected from a file. EOF must be checked too.

Note that the 'return;' is useless.

--
A+

Emmanuel Delahaye
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Can one declare more than one signal on one line? Merciadri Luca VHDL 4 11-01-2010 02:00 PM
difference between scanf("%i") and scanf("%d") ??? perhaps bug inVS2005? =?ISO-8859-1?Q?Martin_J=F8rgensen?= C Programming 18 05-02-2006 10:53 AM
scanf (yes/no) - doesn't work + deprecation errors scanf, fopen etc. =?ISO-8859-1?Q?Martin_J=F8rgensen?= C Programming 185 04-03-2006 02:49 PM
The number name 'System.Web.UI.WebControls' contains more than the maximum number of prefixes. The maximum is 3. mayur ASP .Net 2 07-02-2004 10:35 AM



Advertisments