simple error checking question 
Author Message
 simple error checking question

Hello,

     I'm writing a golf handicap program as a student project and I
understand the instructor will try and "break" the program so I'm trying
to add some error checking routines but am having a problem. It seems like
there should be an easy way to check if an input from the user is not an
integer (obviously a golf score should be a positive integer value). For
instance, in this piece of code:

         printf("Enter %d golf scores, num_scores);
            for( i = 0; i < num_scores; i++ )
               scanf("%d", &this_score[i].value);

               ------
               ------
              ------
  Since this_score is an int, what's the best way to insert a check here
that would basically say:
                       while( this_score[i].value != A POSITIVE INTEGER )
                           printf("error etc. etc.");

   I aplogize if this seems to simple a question for the newsgroup, but I
can't seem to find anything in the FAQ list or in any of my books that
addresses it.
   Thanks in advance!
KJ



Tue, 26 Jan 1999 03:00:00 GMT  
 simple error checking question

Quote:

> Hello,

>      I'm writing a golf handicap program as a student project and I
> understand the instructor will try and "break" the program so I'm trying
> to add some error checking routines but am having a problem. It seems like
> there should be an easy way to check if an input from the user is not an
> integer (obviously a golf score should be a positive integer value). For
> instance, in this piece of code:

>          printf("Enter %d golf scores, num_scores);

      printf("Enter %d golf scores",num_scores); -- hope this is what you meant.

Quote:
>             for( i = 0; i < num_scores; i++ )
>                scanf("%d", &this_score[i].value);

Your questions stems from not understanding the %d in scanf.  Scanf will
ONLY read in decimal values anything else in the input stream causes the
input to stop cold.  Something to note:  scanf returns an int, what are
you doing with it?  Nothing.  This is your first chance to check for
errors.  Scanf (like most library functions) returns a special character
on error (in this case EOF).  It also returns the number of characters
read.

So if you have the following code:

   for(i=0;i<num_scores;i++)
   {
      if(scanf("%d",&this_score[i].value)==EOF)
      {
         printf("Not a decimal value.");
      }
   }

You might want to rewrite the so that it loops back to try the same index
value again until the user gets it right.  Be sure and add some type of
special exit code so the user doesn't get trapped into inputting numbers
if he/she wants to stop.
--
- Bill Cunningham

Any day programming is better than fishing, sex, watersports, work, doing dishes, laundry, etc, etc.

Famous quote (at 3:45 am before project deadline):
"Hey that bug should only take a few 30 hour debugging sessions..."



Sat, 30 Jan 1999 03:00:00 GMT  
 simple error checking question


Quote:

>Scanf (like most library functions) returns a special character
>on error (in this case EOF).  It also returns the number of characters
>read.

Innovative, and completely incorrect:
1.  No library function I can think of returns a character in any event.  Many
of them return an int.  When something like getchar returns, it returns either
the "next character converted to unsigned char converted to int" or the magic
integer value EOF.
2.  EOF is an *integer*, not a character.  It is a negative integer.
3.  scanf returns the number of *conversions* it succeeded at.  Most
relevantly, scanf("%d", &i) will return *ZERO* if the input contains a
letter.  It would return EOF only on end of input, not on merely invalid
input.

Quote:
>You might want to rewrite the so that it loops back to try the same index
>value again until the user gets it right.

And, of course, you might want to read the characters that confused scanf,
possibly calling getchar() until you get a newline.

-s
--

Unix/C Wizard - send mail for help, or send money for consulting!
The *other* C FAQ, the hacker FAQ, et al.  See web page above.
Unsolicited email (junk mail and ads) is unwelcome, and will be billed for.



Sat, 30 Jan 1999 03:00:00 GMT  
 simple error checking question


: >      I'm writing a golf handicap program as a student project and I
: > understand the instructor will try and "break" the program so I'm trying
: > to add some error checking routines but am having a problem. It seems like
[snippity]
: >             for( i = 0; i < num_scores; i++ )
: >                scanf("%d", &this_score[i].value);

: Your questions stems from not understanding the %d in scanf.  Scanf will
[snip]

Your answering at all stems from not reading the FAQ.  :)  Do check out:

12.20:  Why does everyone say not to use scanf()?  What should I use
        instead?



Sat, 30 Jan 1999 03:00:00 GMT  
 simple error checking question



Quote:
>Your questions stems from not understanding the %d in scanf.  Scanf will
>ONLY read in decimal values anything else in the input stream causes the
>input to stop cold.  Something to note:  scanf returns an int, what are
>you doing with it?  Nothing.  This is your first chance to check for
>errors.  Scanf (like most library functions) returns a special character
>on error (in this case EOF).

EOF isn't a character (there is no guarantee that a char can even hold it),
it is a negative int value returned by scanf that indicates that an error
or end-of-file condition occurred before scanf was able to perform a
conversion.

Quote:
> It also returns the number of characters
>read.

It returns the number of input items that were assigned to. If you want
the number of characters read you have to use the %n conversion specifier.

Quote:
>So if you have the following code:

>   for(i=0;i<num_scores;i++)
>   {
>      if(scanf("%d",&this_score[i].value)==EOF)
>      {
>         printf("Not a decimal value.");
>      }
>   }

This code has the problem that is one scanf conversion fails then all
subsequent ones will too because the offending character isn't removed
from the input stream. This is one of the reasons why it is generally
better to read a line using fgets() and convert the value using sscanf,
atoi or strtol.

--
-----------------------------------------


-----------------------------------------



Sat, 30 Jan 1999 03:00:00 GMT  
 simple error checking question

Quote:

> Hello,

>      I'm writing a golf handicap program as a student project and I
> understand the instructor will try and "break" the program so I'm trying
> to add some error checking routines but am having a problem. It seems like
> there should be an easy way to check if an input from the user is not an
> integer (obviously a golf score should be a positive integer value). For
> instance, in this piece of code:

>          printf("Enter %d golf scores, num_scores);
>             for( i = 0; i < num_scores; i++ )
>                scanf("%d", &this_score[i].value);

>                ------
>                ------
>               ------
>   Since this_score is an int, what's the best way to insert a check here
> that would basically say:
>                        while( this_score[i].value != A POSITIVE INTEGER )
>                            printf("error etc. etc.");

>    I aplogize if this seems to simple a question for the newsgroup, but I
> can't seem to find anything in the FAQ list or in any of my books that
> addresses it.
>    Thanks in advance!
> KJ


There is no error checking with scanf. If someone were to enter a float (i.e. 1.5)
it would be converted into an int and would not crash the program. The way I
do error checking if required is to take information in as a character string
, check for unwanted characters, then use the library functions such as
atof to convert the character into a float (aoti cad -> int)
--
Westley L. Hespeth
Speth Software
http://thunder.ocis.temple.edu/ahespeth/speth.html


Sun, 31 Jan 1999 03:00:00 GMT  
 simple error checking question

Quote:

>Hello,

>     I'm writing a golf handicap program as a student project and I
>understand the instructor will try and "break" the program so I'm trying
>to add some error checking routines but am having a problem. It seems like
>there should be an easy way to check if an input from the user is not an
>integer (obviously a golf score should be a positive integer value). For
>instance, in this piece of code:
>         printf("Enter %d golf scores, num_scores);
>            for( i = 0; i < num_scores; i++ )
>               scanf("%d", &this_score[i].value);
>               ------
>               ------
>              ------
>  Since this_score is an int, what's the best way to insert a check here
>that would basically say:
>                       while( this_score[i].value != A POSITIVE INTEGER )
>                           printf("error etc. etc.");
>   I aplogize if this seems to simple a question for the newsgroup, but I
>can't seem to find anything in the FAQ list or in any of my books that
>addresses it.
>   Thanks in advance!
>KJ


KJ, next time you should post a new article rather than a follow-up
article.
If I were your instructor, I would award highest scores to those
student who use direct screen/terminal IO. For examples, use
getc,getch to trap non-numeric character and control characters. And
then write your scanning routine.  This will not be too hard, you can
refer the 'K&R Bible' for information.


Thu, 04 Feb 1999 03:00:00 GMT  
 simple error checking question

: >     I'm writing a golf handicap program as a student project and I
: >understand the instructor will try and "break" the program so I'm trying
: >to add some error checking routines but am having a problem. It seems like
[snip]

: KJ, next time you should post a new article rather than a follow-up
: article.
: If I were your instructor, I would award highest scores to those
: student who use direct screen/terminal IO. For examples, use
: getc,getch to trap non-numeric character and control characters. And
: then write your scanning routine.  This will not be too hard, you can
: refer the 'K&R Bible' for information.

Are you deliberately giving people wrong and half-correct answers?
This is the second in a row I've seen, aside from "as a rule, ignore
compiler warnings", which is, if you haven't realized, incredibly
stupid.

This group discusses C, in which "direct screen I/O" is not defined.



Mon, 08 Feb 1999 03:00:00 GMT  
 simple error checking question


Quote:



>: >     I'm writing a golf handicap program as a student project and I
>: >understand the instructor will try and "break" the program so I'm trying
>: >to add some error checking routines but am having a problem. It seems like
>[snip]

>: KJ, next time you should post a new article rather than a follow-up
>: article.
>: If I were your instructor, I would award highest scores to those
>: student who use direct screen/terminal IO. For examples, use
>: getc,getch to trap non-numeric character and control characters. And
>: then write your scanning routine.  This will not be too hard, you can
>: refer the 'K&R Bible' for information.

>Are you deliberately giving people wrong and half-correct answers?
>This is the second in a row I've seen, aside from "as a rule, ignore
>compiler warnings", which is, if you haven't realized, incredibly
>stupid.

>This group discusses C, in which "direct screen I/O" is not defined.

Here french man !!!

Alors :

/**************************
   Read Bios keyboard
**************************/
int keyb(void)
{
int key,lo,hi;

key = bioskey(0);

lo=key & Ox00FF;

hi=(key & 0xFF00)

return((lo==0)? hi+256:lo);

Quote:
}

Return type examples:

    Backspace -> 8

    Home key 327
    End key  335

    ... etc  

Bye stupide man...



Tue, 09 Feb 1999 03:00:00 GMT  
 simple error checking question



{snip of quoted stuff}

Quote:
> *******************begin r.s. response**********************

>    a)  check return type of scan routine
>    and capture value...

>    should be number effectively scanned...
>    if this number is inappropriate...
>    loop to reprompt and scan...
>    until all input is appropriate...
>    (this is a simple,  but effective method...)

>    b) might want to try using
>            sscanf()
>    and
>            gets()

                       ^^^^^^^^
Or even fgets() if you are not quite as brave...

#ifdef SENSIBLY_CAUTIOUS
#include <see FAQ's sensible rantings on gets()>
#endif

Quote:
>    instead...if
>            scanf()
>    proves intractable (on your system...)
>    .

> *******************end r.s. response************************



Sun, 14 Feb 1999 03:00:00 GMT  
 simple error checking question

: >Hello,
: >  
: >     I'm writing a golf handicap program as a student project and I
: >understand the instructor will try and "break" the program so I'm trying
: >to add some error checking routines but am having a problem. It seems like
: >there should be an easy way to check if an input from the user is not an
: >integer (obviously a golf score should be a positive integer value). For
: >instance, in this piece of code:

: >         printf("Enter %d golf scores, num_scores);
: >            for( i = 0; i < num_scores; i++ )
: >               scanf("%d", &this_score[i].value);

: >               ------
: >               ------
: >              ------
: >  Since this_score is an int, what's the best way to insert a check here
: >that would basically say:
: >                       while( this_score[i].value != A POSITIVE INTEGER )
: >                           printf("error etc. etc.");

: >   I aplogize if this seems to simple a question for the newsgroup, but I
: >can't seem to find anything in the FAQ list or in any of my books that
: >addresses it.
: >   Thanks in advance!
: >KJ

: KJ, next time you should post a new article rather than a follow-up
: article.
: If I were your instructor, I would award highest scores to those
: student who use direct screen/terminal IO. For examples, use
: getc,getch to trap non-numeric character and control characters. And
: then write your scanning routine.  This will not be too hard, you can
: refer the 'K&R Bible' for information.

--
*******************begin r.s. response**********************

        a)  check return type of scan routine
        and capture value...

        should be number effectively scanned...
        if this number is inappropriate...
        loop to reprompt and scan...
        until all input is appropriate...
        (this is a simple,  but effective method...)

        b) might want to try using
                sscanf()
        and
                gets()
        instead...if
                scanf()
        proves intractable (on your system...)
        .

*******************end r.s. response************************
Ralph Silverman



Sun, 14 Feb 1999 03:00:00 GMT  
 simple error checking question

|> >              sscanf()
|> >      and
|> >              gets()
                      ^^^^^^^^
|> Or even fgets() if you are not quite as brave...
                                           ^^^^^
You misspelled "careless".

Hope this helps.

--
* "And I don't like doing silly things (except on purpose)."



Mon, 15 Feb 1999 03:00:00 GMT  
 
 [ 12 post ] 

 Relevant Pages 

1. Question about a simple error check.

2. simple struct error check question

3. newbie error checking question

4. Simple question on floating-point errors

5. a simple simple question...

6. simple simple print question

7. Checking Memory - Simple C Routines

8. Help : Any simple method to check the existence of a file

9. HELP: Simple Check Box

10. Newbie: CATALOG sample question (SIMPLE question)

11. Edit Box error checking

12. keyboard input numeric error checking

 

 
Powered by phpBB® Forum Software