Thread: checks for bad input

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    99

    checks for bad input

    Hi, I am looking for some ideas (for a beginner) how to check for a bad input from user. I already saw an example how to check whether the user typed "1", "2", or "3". However, what about more general check? Like to check whether the input is numerical in general? How do I find out whether the user did not type some litarals?

    Thanks in advance.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    well, there are certain levels of verification - a weak version might be something like:

    Code:
    int isInteger(const char * inp) {
     int i, j, found = FALSE;
     const char * comp = "0123456789";
     for(i = 0; inp[i] != '\0'; ++i) {
     found = FALSE;
      for(j = 0; comp[j] != '\0'; ++j) {
       if(inp[i] == comp[j]) {
        found = TRUE;
        break;
        } 
       }
      if(found == FALSE) {
       break;
       } 
      }  
     return found;
     }
    obviously, a really robust one would account for valid numerical limits, etc.
    Last edited by Sebastiani; 09-09-2004 at 08:05 PM.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User
    Join Date
    Dec 2003
    Posts
    50
    The standard C Library has some useful functions, see ctype.h

    For example:
    #include <ctype.h>
    int isdigit(int c);

    Returns 1 if c is in the set '0' - '9'. Otherwise it returns 0.
    URL ref.:
    http://www.infosys.utas.edu.au/info/...b.html#ctype.h

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    If you don't want to use ctype.h's isdigit() you could do:
    Code:
    int isInteger(const char *inp)
    {
      int i;
    
      for(i = 0;inp[i];i++)
        if(inp[i] <= '0' || inp[i] >= '9')
          break;
      return !inp[i];
    }
    Of course...it would be better just #include <ctype.h> and then replace if(inp[i] <= '0' || inp[i] >= '9') with if(isdigit(inp[i]))

    EDIT: Beaten to the punch on isdigit(). Now I know how quzah feels.
    If you understand what you're doing, you're not learning anything.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Of course...it would be better just #include <ctype.h> and then replace
    Of course, it would be even better to go all out and perform a stronger check:
    Code:
    #include <errno.h>
    #include <limits.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int is_integer ( const char *s )
    {
      char *end;
      long result;
      
      errno = 0;
      result = strtol ( s, &end, 0 );
    
      /* Incomplete conversion */
      if ( *s != '\0' && *end != '\0' )
        return 0;
      /* Overflow/Underflow */
      if ( ( result == LONG_MAX || result == LONG_MIN ) && errno == ERANGE )
        return 0;
      /* Out of range */
      if ( result > INT_MAX || INT_MIN > result )
        return 0;
    
      return 1;
    }
    
    int main ( void )
    {
      char buffer[BUFSIZ];
    
      while ( fgets ( buffer, sizeof buffer, stdin ) != NULL ) {
        buffer[strcspn ( buffer, "\n" )] = '\0';
        printf ( "%s -- %d\n", buffer, is_integer ( buffer ) );
      }
    
      return 0;
    }
    My best code is written with the delete key.

  6. #6
    Registered User lobo's Avatar
    Join Date
    Oct 2001
    Posts
    71
    Nice job, Prelude

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by Prelude
    >Of course...it would be better just #include <ctype.h> and then replace
    Of course, it would be even better to go all out and perform a stronger check:
    That's true, but the OP didn't mention anything about converting the string to an integer, just to check if the string was all digits. That's why I didn't check limits, etc. It might even be prohibitive to what he's trying to do.
    If you understand what you're doing, you're not learning anything.

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    99
    Thanks guys a lot! I am working on these now. I really appreciate. I am a beginner, so it will take some time
    Last edited by kocika73; 09-10-2004 at 10:05 AM.

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >just to check if the string was all digits
    Well, technically the question was how to check if a string represents a numerical value. In that case even my stronger check isn't sufficient because floating-point values are numeric and can contain characters other than digits and signs (which I might add, you neglected to check for ). There is also the issue of formatting. -1234 is a valid number but 123-4 is not unless - is being used as a decimal marker. So just because the string contains the requisite characters doesn't mean that it's a valid number. Checking limits was just an after thought because all examples prior had implemented an is_integer rather than is_numeric. The key point is that strtol handles all of the nasty format checking.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. input redirection
    By sashaKap in forum C Programming
    Replies: 6
    Last Post: 06-25-2009, 01:59 AM
  2. For loop problems, input please.
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-22-2007, 03:54 AM
  3. I would love some input on my BST tree.
    By StevenGarcia in forum C++ Programming
    Replies: 4
    Last Post: 01-15-2007, 01:22 AM
  4. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  5. Help with Input Checking
    By Derek in forum C Programming
    Replies: 7
    Last Post: 06-17-2003, 03:07 AM