[HELP] functions to detect non-numerical

This is a discussion on [HELP] functions to detect non-numerical within the C Programming forums, part of the General Programming Boards category; If I wanted the user to enter 2 numbers to be multiply/add, if user enter something like A5, I need ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    29

    [HELP] functions to detect non-numerical

    If I wanted the user to enter 2 numbers to be multiply/add, if user enter something like A5, I need a function to detect the A, and tell the user non-numerical found. thanks.
    Last edited by Dakaa; 05-30-2009 at 02:38 AM.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,638
    >> I need a function to detect the A

    Use isdigit() to test each character.
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Time to investigate the ASCII table. You can distinguish characters easily using their numerical value:

    char 'a' = 97
    char '5' = 53

    These are all values between 0 and 127, which is enough to hold both cases of the English alphabet, digits 0-9, punctuation, and a few other things. If you look at it you will be able to see a way to prune non-digits.

    I think there is a command too...
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,638
    >> Time to investigate the ASCII table. You can distinguish characters easily using their numerical value:

    There are three basic ways to do it:
    1) isdigit; Portable. Locale independant.
    2) Literal comparison, eg: in the range '0' - '9'; Portable. Locale dependant.
    3) 'Magic constant' comparison, eg: in the range 0x30 - 0x39; Non-portable. Locale dependant.

    I'd go with #1, personally.
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I like #2.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    2? but then don't try to
    Code:
    if ('0' <= c <= '9') ...
    I'd use isdigit, since it is probably the most portable. What if a character set stores '0' as, say, 89 and '1' to '9' as 111 to 119? That's not likely, but it could happen.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    Quote Originally Posted by Brafil View Post
    2? but then don't try to
    Code:
    if ('0' <= c <= '9') ...
    I'd use isdigit, since it is probably the most portable. What if a character set stores '0' as, say, 89 and '1' to '9' as 111 to 119? That's not likely, but it could happen.
    Actually, it cannot happen for a standard conforming implementation since it is guaranteed that '0' + 1 == '1', '1' + 1 == '2', ..., '8' + 1 == '9'.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    Quote Originally Posted by laserlight View Post
    Actually, it cannot happen for a standard conforming implementation since it is guaranteed that '0' + 1 == '1', '1' + 1 == '2', ..., '8' + 1 == '9'.
    OK. The one with the biggest mouth on earth spoke again (-> me). Ignore my post.

  9. #9
    Registered User
    Join Date
    Apr 2009
    Posts
    29
    isdigit() worked for char...

    What if I had

    int num1;

    and user enter an alphabet for num1?
    Last edited by Dakaa; 05-30-2009 at 07:39 PM.

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Dakaa View Post
    and user enter an alphabet for num1?
    Depends how you are gathering the input. A safe way is to take everything as a string and use strtol(); if you are reading numbers from a text file (an almost unavoidable task), you will have to do that, so you need to learn it sometime.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,638
    Well you need some what to get the value into num1 in the first place, so you might use scanf et al and check the return value, or fgets and loop throught the string, testing each character with isdigit.
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  12. #12
    Registered User
    Join Date
    Apr 2009
    Posts
    29
    my mickey mouse code, trying to use isdigit() to make sure int bal, wit and dep are numbers, so I have to turn bal,wit and dep to char?

    Code:
    #include <conio.h>
    #include <stdio.h>
    
    int main()
    {
      int i,bal,wit,dep;
      char key[2];
      
      for(;;)
      {
        bal=0;
        wit=0;
        dep=0;
        
        for(i=0;i<=2;i++)
        {
          key[i]='\0';
        }
      
        printf("[W/D]? ");
        key[i]=getch(); //getche() works as well
    
        if(key[i]=='W'||key[i]=='w')
        {
          printf("\nenter opening balance: ");
          scanf("%i",&bal);
          printf("withdraw amount: ");
          scanf("%i",&wit);
    
          if((bal-wit)>-201)
          {
            printf("closing balance: %i\n\n",(bal-wit));
          }
    
          else
          {
            printf("not allowed\n\n"); //no overdraft more than 200
          }
    
        }
      
        else if(key[i]=='D'||key[i]=='d')
        {
          printf("\nenter opening balance: ");
          scanf("%i",&bal);
          printf("deposit amount: ");
          scanf("%i",&dep);
          printf("closing balance: %i\n\n",(bal+dep));
        }
      
        else
        {
          printf("\ntry again\n\n");
        }
        
      }
      
      getch();
      return 0;
    }
    EDIT: I find this code stupid, because all I know is how to use loops, my teacher is total useless.

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I don't think you've quite got what we're saying. Once you say "scanf" you've already lost the battle -- either you've read an int and there's nothing to check, or they typed a character, in which case scanf gave you nothing and you have nothing to check.

    You need to read the input as it is, with say fgets, and then check what you've got.

  14. #14
    Registered User
    Join Date
    Apr 2009
    Posts
    29
    Replacing scanf with fgets, it says,

    1. invalid conversion from `int*' to `int'

    2. too few arguments to function `char* fgets(char*, int, FILE*)'

    3. at this point in file

  15. #15
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Dakaa View Post
    trying to use isdigit() to make sure int bal, wit and dep are numbers, so I have to turn bal,wit and dep to char?
    Actually, isdigit() is not in the code you posted.

    Quote Originally Posted by tabstop View Post
    Once you say "scanf" you've already lost the battle -- either you've read an int and there's nothing to check, or they typed a character, in which case scanf gave you nothing and you have nothing to check.
    Beyond that, there is nothing a character can hold that an integer cannot, so you may end up with a value in the int that corresponds to a character (google "ASCII table") and this will still be a "digit". isdigit() would test if that integer ascii value equates to the characters 0-9 (the int value is usually 48-57). This is why I recommended getting the number in a string (eg, "1") and using strtol() or the easier but "less secure" atoi().
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  2. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 02:35 AM
  3. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  4. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM
  5. functions - please help!!!!
    By linkies in forum C Programming
    Replies: 1
    Last Post: 08-21-2002, 07:53 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21