Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   how to check the scanf function if it will read more than one number (http://www.velocityreviews.com/forums/t440728-how-to-check-the-scanf-function-if-it-will-read-more-than-one-number.html)

moosdau 01-02-2006 10:05 AM

how to check the scanf function if it will read more than one number
 
my code:
do
{
printf("please input the dividend and the divisor.\n");
if(!scanf("%d%d",&dend,&dor))
{
temp1=1;
fflush(stdin);
}
else
temp1=0;
}while(temp1==1);

it seems that it only depend on the first number it read.
if I input " a 32 ", it could know there is a error,
but if I input " 32 a ",
it accept that.

thanks in advance.


tmp123 01-02-2006 10:15 AM

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

moosdau wrote:
> my code:
> do
> {
> printf("please input the dividend and the divisor.\n");
> if(!scanf("%d%d",&dend,&dor))
> {
> temp1=1;
> fflush(stdin);
> }
> else
> temp1=0;
> }while(temp1==1);
>
> it seems that it only depend on the first number it read.
> if I input " a 32 ", it could know there is a error,
> but if I input " 32 a ",
> it accept that.
>
> thanks in advance.



Hi,

In "man scanf", I suggest you to read chapter "return values".

Kind regards.


moosdau 01-02-2006 10:32 AM

Re: how to check the scanf function if it will read more than one number
 
do you mean the linux os?
but I'm using windows,so I can't use the "man" command.
could you tell me plz?


=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?= 01-02-2006 10:39 AM

Re: how to check the scanf function if it will read more than onenumber
 
moosdau wrote:
> do you mean the linux os?
> but I'm using windows,so I can't use the "man" command.
> could you tell me plz?

For windows, look up the function at
http://msdn.microsoft.com/

(A little hint is that scanf returns the nr of successfully
assigned items)

Keith Thompson 01-02-2006 10:44 AM

Re: how to check the scanf function if it will read more than onenumber
 
"moosdau" <moosdau@gmail.com> writes:
> my code:
> do
> {
> printf("please input the dividend and the divisor.\n");
> if(!scanf("%d%d",&dend,&dor))
> {
> temp1=1;
> fflush(stdin);
> }
> else
> temp1=0;
> }while(temp1==1);
>
> it seems that it only depend on the first number it read.
> if I input " a 32 ", it could know there is a error,
> but if I input " 32 a ",
> it accept that.


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
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.

scanf() can be very tricky, it can leave unread garbage in your input
stream, and it can be difficult to figure out just what it's doing. A
better approach is to use fgets() to read an entire line, then use
sscanf() to get information from the line you've read.

--
Keith Thompson (The_Other_Keith) kst-u@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.

tmp123 01-02-2006 10:45 AM

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

moosdau wrote:
> do you mean the linux os?
> but I'm using windows,so I can't use the "man" command.
> could you tell me plz?


Mna pages are also available in hundreds of www servers.


Chuck F. 01-02-2006 12:07 PM

Re: how to check the scanf function if it will read more than onenumber
 
moosdau wrote:
>
> do you mean the linux os? but I'm using windows,so I can't use
> the "man" command. could you tell me plz?


Include context. Without it we have no idea what you mean. See my
sig, and the reference therein, for how to do this.

On Windoze you are probably either using Microsoft VC, or some port
of GCC. With the gcc versions you very likely have info (the
command) available, especially if you have installed DJGPP or
Cygwin. Otherwise the VC help system will probably lead you to a
discussion of scanf.

On this windoze system the command:

C>info libc alpha scanf

brings up a screen that begins as follows:

scanf
=====

Syntax
------

#include <stdio.h>

int scanf(const char *format, ...);

Description
-----------

This function scans formatted text from `stdin' and stores it in
the variables pointed to by the arguments. *Note scanf::.
.... and so forth ...

--
"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/>

moosdau 01-02-2006 12:16 PM

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

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 ......

> 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?


> scanf() can be very tricky, it can leave unread garbage in your input
> stream, and it can be difficult to figure out just what it's doing. A
> better approach is to use fgets() to read an entire line, then use
> sscanf() to get information from the line you've read.


thanks for your suggestion!


> --
> Keith Thompson (The_Other_Keith) kst-u@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.



Richard Heathfield 01-02-2006 01:12 PM

Re: how to check the scanf function if it will read more than one number
 
moosdau said:

<snip>

> [...] 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,


Because fflush's behaviour is only defined for streams open for output or
update.

> could I use it to clear the input stream?


Not as far as the C language is concerned, no.

> if not , could you please tell me why?


For the same reason that pressing the handle on your toilet will not get rid
of the water waiting in your sink's tap (or faucet, if you're on that side
of the pond). Flushing is something we do to output, not to input.

If you want to discard from stdin everything up to and including a
particular character, you can use this function:

#include <stdio.h>

int DiscardFromStream(FILE *fp, int lim)
{
int ch;
while((ch = getc(fp)) != lim && ch != EOF)
{
continue;
}
return ch == EOF;
}

/* example usage */
int main(void)
{
int ch;
puts("Type a letter, and press ENTER.");
ch = getchar();
if(0 == DiscardFromStream(stdin, '\n'))
{
printf("You pressed %c\n", ch);
puts("Type another letter, and press ENTER.");
ch = getchar();
if(0 == DiscardFromStream(stdin, '\n'))
{
printf("You pressed %c\n", ch);
}
}
return 0;
}

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

pemo 01-02-2006 01:23 PM

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

"moosdau" <moosdau@gmail.com> wrote in message
news:1136204207.764547.227070@o13g2000cwo.googlegr oups.com...
>


>
> 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?


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





All times are GMT. The time now is 04:08 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.