Newbie with scanf question 
Author Message
 Newbie with scanf question

How do I do this without scanf

puts("Please enter an integer");
scanf("%d",&number);

If I type in a letter by accident the program will terminate. Correct?
How would I do the above without scanf??



Mon, 21 Apr 2003 10:49:50 GMT  
 Newbie with scanf question

Quote:

> How do I do this without scanf

> puts("Please enter an integer");
> scanf("%d",&number);

> If I type in a letter by accident the program will terminate. Correct?

No.  scanf() will return zero.

Quote:
> How would I do the above without scanf??

fgets() then sscanf() or strtol().
--
"Give me a couple of years and a large research grant,
 and I'll give you a receipt." --Richard Heathfield


Mon, 21 Apr 2003 11:39:38 GMT  
 Newbie with scanf question

Quote:

> How do I do this without scanf
> puts("Please enter an integer");
> scanf("%d",&number);
> If I type in a letter by accident the program will terminate. Correct?
> How would I do the above without scanf??

use fgets() and sscanf(), i guess:

        char buf[30];

        puts("Please enter an integer");
        fflush(stdout);
        if (! fgets(buf, sizeof buf, f)) error;
        if (sscanf(buf, "%d", &number) != 1)
                possibly_typed_in_letter_by_accident;

alternatively, you could use strtol() instead of sscanf(), but it's a little
bit more work (IMO) using strtol() to determine if bad input has been given
and not quite as clear.  on the plus side, it might be negligibly faster.
the strtol code would look like so:

        char buf[30], *endpt;

        /* ... */
        if (! (fgets(buf, sizeof buf, f)) error;
        number = strtol(buf, &endpt, 10);
        if (endpt == buf)
                possibly_type_in_letter_by_accident;

--
 /"\                                                 m i k e   b u r r e l l

  X        AGAINST HTML MAIL,



Mon, 21 Apr 2003 11:45:08 GMT  
 Newbie with scanf question
On Wed, 1 Nov 2000 21:49:50 -0500, "#include<stdio.h>"

Quote:
> How do I do this without scanf

> puts("Please enter an integer");
> scanf("%d",&number);

> If I type in a letter by accident the program will terminate. Correct?
> How would I do the above without scanf??

See the following web pages:

Jack Klein
--
Home: http://jackklein.home.att.net
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ http://www.faqs.org/faqs/C-faq/learn/



Mon, 21 Apr 2003 11:49:53 GMT  
 Newbie with scanf question
On Wed, 1 Nov 2000 21:49:50 -0500, "#include<stdio.h>"

Quote:
> How do I do this without scanf

> puts("Please enter an integer");
> scanf("%d",&number);

> If I type in a letter by accident the program will terminate. Correct?
> How would I do the above without scanf??

Let's try that again!

See the following web pages with example code:

http://home.att.net/~jackklein/ctips01.html#safe_gets

http://home.att.net/~jackklein/c/code/strtol.html

http://home.att.net/~jackklein/c/code/getint.html

Jack Klein
--
Home: http://jackklein.home.att.net
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ http://www.faqs.org/faqs/C-faq/learn/



Mon, 21 Apr 2003 11:51:08 GMT  
 Newbie with scanf question

Quote:
>How do I do this without scanf

>puts("Please enter an integer");
>scanf("%d",&number);

>If I type in a letter by accident the program will terminate. Correct?

Nope.  If the letter is not the first character, scanf will happily
convert the digits preceding it and leave it in the input buffer, to be
read by the next scanf, which will fail and leave it there for the next
scanf and so on... :-(

scanf is a function that returns the number of successful conversions
performed, 1 in your case, or 0 if the conversion failed because of the
wrong character.

Quote:
>How would I do the above without scanf??

Other people have already mentioned alternatives (which may or may not
be too esoteric for you).  I'll show you how to do it with scanf:

    int rc;

    do {
        rc = scanf("%d", &number);
        while (getchar() != '\n') ;  /* get rid of the characters left */
    } while (rc < 1);                /* by scanf in the input buffer   */

This assumes that you never press your end-of-file key when the program
expects input from you.  Otherwise, things get slightly more complicated:
you have to check if either scanf or getchar has returned EOF and abort
the program (or take whatever action is appropriate) if this happens.

Dan
--
Dan Pop
CERN, IT Division

Mail:  CERN - IT, Bat. 31 1-014, CH-1211 Geneve 23, Switzerland



Mon, 21 Apr 2003 13:02:47 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Newbie Array/Scanf question

2. SCANF() NEWBIE

3. Newbie: Trouble with scanf - HELP!!!

4. Newbie problem w/ scanf

5. Trouble with scanf() (newbie here)

6. newbie problem with scanf

7. Newbie Scanf Puzzles

8. scanf problem (DATA/MEMORY CONCEPT) (newbie)

9. Newbie Array/Scanf Problem

10. Newbie - while loop & scanf problems

11. Newbie - scanf and while loop - correction

12. scanf or n/scanf

 

 
Powered by phpBB® Forum Software