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

 
 
moosdau
Guest
Posts: n/a
 
      01-02-2006
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.

 
Reply With Quote
 
 
 
 
tmp123
Guest
Posts: n/a
 
      01-02-2006

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.

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

 
Reply With Quote
 
=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=
Guest
Posts: n/a
 
      01-02-2006
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)
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      01-02-2006
"moosdau" <(E-Mail Removed)> 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) 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
 
tmp123
Guest
Posts: n/a
 
      01-02-2006

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.

 
Reply With Quote
 
Chuck F.
Guest
Posts: n/a
 
      01-02-2006
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/>
 
Reply With Quote
 
moosdau
Guest
Posts: n/a
 
      01-02-2006

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) (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
 
Richard Heathfield
Guest
Posts: n/a
 
      01-02-2006
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)
 
Reply With Quote
 
pemo
Guest
Posts: n/a
 
      01-02-2006

"moosdau" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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



 
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