Question about a simple error check. 
Author Message
 Question about a simple error check.

Greetings,
        I have a simple question about error checking in loops.

I want to have user input via the keyboard and have the code check to make
sure that the input is a numeric (0-9) character.

The screen (so far) looks something like this:

15 14 10 16 14

What do you want to assign the 15 to?

1.S
2.D
3.W
4.I
5.C
6.O

Now i have error checking in place to make sure that no choice gets chosen
twice.  The problem I run into is that if the user enters any other
keystroke other than what I expect (0-9), the whole thing goes into a loop.

The code in question is as follows.  

for (q=0; q < 6; q++)
         {
          do_clear_screen();
          printf ("Q= %d\n\r",q);
          printf ("Choice = %d\n\r",choice);
          printf ("Dice Rolls:  ");

          printf ("%d    %d    %d    %d    %d    
%d\n\r\n\r",Roll[0],Roll[1],Roll[2],Roll[3],Roll[4],Roll[5]);

          printf ("Which ability do you want to assign the
%d?\n\r\n\r",Roll[q]);    

          if (STR == 0)
           printf ("1. Strength\n\r");
          else
           printf ("\t\t\tStrength: %d\n\r",STR);

          if (DEX == 0)
           printf ("2. Dexterity\n\r");
          else
           printf ("\t\t\tDexterity: %d\n\r",DEX);

          if (INT == 0)
           printf ("3. Intelligence\n\r");
          else
           printf ("\t\t\tIntelligence: %d\n\r",INT);

          if (WIS == 0)
           printf ("4. Wisdom\n\r");
          else
           printf ("\t\t\tWisdom: %d\n\r",WIS);

          if (CON == 0)
           printf ("5. Constitution\n\r");
          else
           printf("\t\t\tConstitution: %d\n\r",CON);

          if (CHA == 0)
           printf ("6. Charisma\n\r");
          else
           printf("\t\t\tCharisma: %d\n\r",CHA);      

         scanf("%d", &choice);

          switch (choice)
          {

          case 1:
                if (STR == 0)
                 STR = Roll[q];
                else
                 q--;
                break;

          case 2:
                if (DEX == 0)
                 DEX = Roll[q];
                 else
                 q--;
                break;

          case 3:
                if (INT == 0 )
                 INT = Roll[q];
                else
                 q--;
                break;

          case 4:
                if (WIS == 0 )
                 WIS = Roll[q];
                else
                 q--;
                break;

          case 5:
                if (CON == 0 )
                 CON = Roll[q];
                else
                 q--;
                break;

          case 6:
                if (CHA == 0 )
                 CHA = Roll[q];
                else
                 q--;
                break;

          default:
                q--;
                break;
          }
        }

Now I am far from an expert coder and this is probably something simple and
this is probably not the best way to do this, so please go easy on me.

                                                Keith
--



Sun, 02 Mar 2003 03:41:41 GMT  
 Question about a simple error check.

writes

Quote:
>Now I am far from an expert coder and this is probably something simple
>and this is probably not the best way to do this, so please go easy on
>me.

You must remember that you have an idiot user who can hit any key on the
keyboard so get the response as a string (check using fgets()) now you
can look at the first character and if it is not a digit you can reject
it and ask the user to try again.

Francis Glassborow      Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
--



Sun, 02 Mar 2003 22:13:43 GMT  
 Question about a simple error check.

Quote:

>          scanf("%d", &choice);

Don't use scanf when you need to validate input.
Instead, input text characters into a buffer array
then apply validation tests to that buffer (e.g.
invoke strtod and see if the result is 0).
--



Sun, 02 Mar 2003 22:13:59 GMT  
 Question about a simple error check.

Quote:

> for (q=0; q < 6; q++)
>          {
>      do_clear_screen();

<g>

Quote:
>     scanf("%d", &choice);

Don't use scanf() for this. You're quite correct, it will leave unusable
characters in the input stream, and unless you're careful, it can easily
block. A better solution is to use fgets(), and then to apply strtoul()
to the result.
Read the comp.lang.c FAQ, it's instructive, and it has some FAQs about
this very problem: <http://www.eskimo.com/~scs/C-faq/q12.19.html>, and
also the rest of that section.

Richard
--



Sun, 02 Mar 2003 22:14:21 GMT  
 Question about a simple error check.

Quote:

> Greetings,
>            I have a simple question about error checking in loops.
> I want to have user input via the keyboard and have the code check to make
> sure that the input is a numeric (0-9) character.
> Now i have error checking in place to make sure that no choice gets chosen
> twice.  The problem I run into is that if the user enters any other
> keystroke other than what I expect (0-9), the whole thing goes into a loop.
> The code in question is as follows.  

[snip]

Quote:
>     scanf("%d", &choice);

IMHO I would suggest using something like fgets, which
(a) is not likely to get confused with characters (conversion to int?)
(b) does not have problems with extra-long strings like gets

IME, scanf works in well-defined cases where you *know* what the
input/output will be like, but is less tolerant with 'wrong' input.

Quote:
> Now I am far from an expert coder and this is probably something simple and
> this is probably not the best way to do this, so please go easy on me.
>From the rest of the code this is 'verbose' coding where elements of

(blocks of) code appear easy to collapse by appropriate uses of arrays.
So I'd have an array to hold the character attributes (STR,..), a similar
one to hold the attribute strings, and possible an enumeration to enable
array-indexing to be easier. Apart from shrinking the amount of code you
need to *write*, it also makes maintenance easier IMO, since you only have
one section to modify!

Then the print-out loop could be something like:
char i;
for(i=0;i<6;i++)
{
  if(Attribute[i]==0)
    printf("%d. %s\n\r",i,AttributeString[i]);
  else                
    printf("\t\t\t%s: %d\n\r",AttributeString[i],Attribute[i]);

Quote:
}

The switch could also be replaced with something like:

scanf("%d",i); /* or something else! */
if(i<1 || i>6) { /* possibly print error message */}
else
{
  if(Attribute[i]==0) Attribute[i]=Roll[q];
  else q--;

Quote:
}

Yes, the code layout isn't ideal, but hopefully you get the idea :)

HTH,

--
Neil
--



Sun, 02 Mar 2003 22:14:27 GMT  
 Question about a simple error check.
: Greetings,
:       I have a simple question about error checking in loops.

: I want to have user input via the keyboard and have the code check to make
: sure that the input is a numeric (0-9) character.

strtol(), strtoul(), strtod() all have error checking capabilities.  Try
this sample code to see how it works:

#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <errno.h>

int main( void )
{
    char *data = "1W 2   3";
    char *end_ptr;
    long value;

    errno = 0;
    value = strtol( data, &end_ptr, 10 );
    if( end_ptr == data || ('\0' != *end_ptr && !isspace( (int)*end_ptr)) )
         fprintf( stderr,
          "Empty or non digit characters in conversion string: \"%s\"\n",
                          end_ptr );
    if( errno )
        perror( "strtol()" );

    printf( "Value of \"%s\" is %ld\n", data, value );
    exit( EXIT_SUCCESS );

Quote:
}

Good Luck,

Paul

--
Paul D. Boyle

North Carolina State University
http://laue.chem.ncsu.edu/web/xray.welcome.html
--



Sun, 02 Mar 2003 22:14:34 GMT  
 Question about a simple error check.

Quote:

> Greetings,
>         I have a simple question about error checking in loops.
> I want to have user input via the keyboard and have the code check to make
> sure that the input is a numeric (0-9) character.
>  The problem I run into is that if the user enters any other
> keystroke other than what I expect (0-9), the whole thing goes into a loop.

Is that really a C question ?

If I would describe the needed functionality, I'd say it is:

Keyboard_input = Invalid.
do {
        Get_New_Keyboard_Input

Quote:
} while (KeyBoard_input == Invalid)

That is, you *must* loop until you get valid input. However, I would implement
that not as you did. I could use the return value of scanf(). It will signal if
a valid input was detected (1 conversion performed if 0..9, 0 if invalid).

If the input is invalid, not matchable, scanf doesn't skip it. How would it
know how much to skip? If you need new input ,as you did in your algorithm,
you should read and discard the input line in the case you didn't get valid
input.

Another approach is to use fgets(). It will consume an entire line of input,
assumed that that fits in your buffer, and nothing gets stuck. You then look
in the buffer to see if you got "0\0" or "1\0" or something like that. I'd
test first if the second character was \0, else I'd echo "1 digit #, please"
and then switch on the first character of the buffer. Note, the first
character should be '0' to '9' (characters), not 0 to 9 (integers). E.g.
on ASCII '0' == 0x30 != 0x00, and thus case ('1'): if (STR == 0) ...

Michiel Salters
--



Mon, 03 Mar 2003 03:00:00 GMT  
 Question about a simple error check.

Quote:
>Greetings,
>         I have a simple question about error checking in loops.

>I want to have user input via the keyboard and have the code check to
>make sure that the input is a numeric (0-9) character.

>and this is probably not the best way to do this, so please go easy on
>me.

>                                                      Keith

Thanks to all who have offered advice and solutions.  It is greatly
appreciated.

                Keith
--



Wed, 05 Mar 2003 03:00:00 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. simple error checking question

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