Scanf troubles 
Author Message
 Scanf troubles

I'm having a problem with the scanf function.  In my program I
have the following bit of code:

         int result;
         scanf("%d", &result);

It works fine as long as the user types a series of digit keys (0-9).
However, if the user types an alpha key, the whole thing goes crazy.
The program goes into an infinite loop and repeatedly blows right
through scanf.

I've tried everything I can think of to deal with this "type" error.
Any suggestions will be greatly appreciated.  Thanks!

--
Jim Borland
Construction Management Department
Cal Poly University, San Luis Obispo



Mon, 03 Feb 1997 10:12:31 GMT  
 Scanf troubles


Quote:
> I'm having a problem with the scanf function.  In my program I
> have the following bit of code:

>          int result;
>          scanf("%d", &result);

> It works fine as long as the user types a series of digit keys (0-9).
> However, if the user types an alpha key, the whole thing goes crazy.
> The program goes into an infinite loop and repeatedly blows right
> through scanf.

Use fgets() to get a line of input (using stdin for the stream argument, and
please don't use gets() instead of fgets()... ), and then go through it using
sscanf(), or such functions as strtol(). The man page or your compiler's manual
will give you all the details.

--
                                       ^^

University of Twente                 =x=  \        tel. +31 53 893747
Tele-Informatics & Open Systems        |   \       tfx. +31 53 333815
P.O. Box 217   7500 AE Enschede       /|__  \
The Netherlands                      (____)_/

"Spare no expense to save money on this one."
                -- Samuel Goldwyn



Mon, 03 Feb 1997 17:47:01 GMT  
 Scanf troubles

Quote:

>I'm having a problem with the scanf function.  In my program I
>have the following bit of code:

>         int result;
>         scanf("%d", &result);

Jim,

Check the return code on scanf.  If you enter non-numeric,
scanf should return a 0 and not store anything in result.

David Boyd                                   Roadnet Technologies, Inc.

-----------------------------------------------------------------
Disclaimer: Opinions expressed in this posting do not represent the views of
            Roadnet Technologies, Inc., nor United Parcel Service.  



Tue, 04 Feb 1997 01:30:56 GMT  
 Scanf troubles

Quote:


>> I'm having a problem with the scanf function.  In my program I
>> have the following bit of code:

>>          int result;
>>          scanf("%d", &result);

>> It works fine as long as the user types a series of digit keys (0-9).
>> However, if the user types an alpha key, the whole thing goes crazy.
>> The program goes into an infinite loop and repeatedly blows right
>> through scanf.

>Use fgets() to get a line of input (using stdin for the stream argument, and
>please don't use gets() instead of fgets()... ), and then go through it using
>sscanf(), or such functions as strtol(). The man page or your compiler's manual
>will give you all the details.

Or fgets() and then check the first char received and then atoi().

/Mattias

--

*-----------------------------------------------------------------------------*
           "It makes you wonder...what you bothered for..."
                                                      curve



Tue, 04 Feb 1997 07:59:33 GMT  
 Scanf troubles

Quote:



>>> I'm having a problem with the scanf function.  In my program I
>>> have the following bit of code:

>>>          int result;
>>>          scanf("%d", &result);

>>> It works fine as long as the user types a series of digit keys (0-9).
>>> However, if the user types an alpha key, the whole thing goes crazy.
>>> The program goes into an infinite loop and repeatedly blows right
>>> through scanf.

>>Use fgets() to get a line of input (using stdin for the stream argument, and
>>please don't use gets() instead of fgets()... ), and then go through it using
>>sscanf(), or such functions as strtol(). The man page or your compiler's
>>manual will give you all the details.

>Or fgets() and then check the first char received and then atoi().

What do you mean by "checking the first char"?  Valid input for atoi()
is [\t ]*[+-]?[0-9]+  (pardon my regexpr).  You can check this, but
it's not as simple as calling isdigit().

--
Miguel Carrasquer         ____________________  ~~~
Amsterdam                [                  ||]~  



Tue, 04 Feb 1997 08:48:29 GMT  
 Scanf troubles

Quote:

> Or fgets() and then check the first char received and then atoi().

Or fgets() and then strtol, which should give you the best error checking
potential (including overflow).

--

ftp.cs.helsinki.fi:pub/Software/Local/Publib -- general C function library



Tue, 04 Feb 1997 21:04:29 GMT  
 Scanf troubles

Quote:

>>Or fgets() and then check the first char received and then atoi().
>What do you mean by "checking the first char"?  Valid input for atoi()
>is [\t ]*[+-]?[0-9]+  (pardon my regexpr).  You can check this, but
>it's not as simple as calling isdigit().

Leaving aside the regexp, there is a simpler way -- use strtol() making
full use of all its arguments and check what it stores in the object
pointed to by its 2nd argument if the return value is one of zero,
LONG_MAX or LONG_MIN.  See ISO section 7.10.1.5 for the full scoop.

--

681 Park Street, Brunswick, Vic. 3056, Australia



Wed, 05 Feb 1997 04:27:01 GMT  
 Scanf troubles

Quote:

> I'm having a problem with the scanf function.  In my program I
> have the following bit of code:

>          int result;
>          scanf("%d", &result);

> It works fine as long as the user types a series of digit keys (0-9).
> However, if the user types an alpha key, the whole thing goes crazy.
> The program goes into an infinite loop and repeatedly blows right
> through scanf.

> I've tried everything I can think of to deal with this "type" error.
> Any suggestions will be greatly appreciated.  Thanks!

Try adding this to your CFLAGS line in the Makefile:

    -Dscanf=DONT_USE_SCANF

(Thanks, D'Arcy! :)

Scanf behaves badly when given unexpected input, as you have noticed.
(did you also notice that the next time you call scanf after a
successful number entry, it doesn't seem to work?)  Use fgets() to pull
the input into a buffer, and sscanf() to parse it.  You can test the
return value of sscanf() to see if you got a good read.
--

head -2 /usr/philosophy/survival  |      PGP 2.6 public key
#! /usr/local/bin/perl -n         |      available upon request
next unless /$clue/;              |      (send yours)



Mon, 03 Feb 1997 15:26:21 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. scanf troubles

2. printf/scanf trouble on SPARCstation

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

4. Trouble with scanf() (newbie here)

5. trouble bulltproofing scanf

6. Troubles using C's scanf Function

7. trouble with scanf?

8. Trouble with scanf( "...%[^...]", ...)

9. scanf or n/scanf

10. newbie trouble: CoCreateInstance Error + IWebBrowser trouble...

11. (novice) I'm Having trouble scanf'ing a double ?

12. scanf("%s", string) or scanf("%s", &string)? Both work, yet...

 

 
Powered by phpBB® Forum Software