Numeric Checking in C++

Sharky Forums


Results 1 to 10 of 10

Thread: Numeric Checking in C++

  1. #1
    Hammerhead Shark Raptor2150's Avatar
    Join Date
    Aug 2001
    Posts
    1,507

    Post Numeric Checking in C++

    I'm looking for something along the guidelines of IsNumeric() in VB for C++. Yeah, I'm a vb programmer

    ------------------
    Athlon XP 1800+ (OC pending)
    Alpha PAL8045
    Epox 8KHA+
    256mb Crucial pc2100 ddr
    Maxtor D740x 40 GB
    ATI Radeon8500 (300/300)
    Antec PP403x 400w

  2. #2
    Mako Shark dighn's Avatar
    Join Date
    Nov 2000
    Location
    Vancouver, BC Canada
    Posts
    3,171

    Post

    .

    EDIT: don't know (sorry)

    ------------------
    Keep it brief

    [This message has been edited by dighn (edited January 20, 2002).]
    .

  3. #3
    Texan Dragon Moderator Galen of Edgewood's Avatar
    Join Date
    Sep 2000
    Location
    Ft Irwin, CA
    Posts
    5,602

    Post

    Originally posted by Raptor2150:
    I'm looking for something along the guidelines of IsNumeric() in VB for C++. Yeah, I'm a vb programmer

    What is it that you are trying to accomplish? We might be able to code somethin' for ya.



    ------------------
    Dragon of the OC Crusaders

    I only chewed in self-defense.
    Dragon of the OC Crusaders

    Break the rules and you're snack food for this dragon...

  4. #4
    Hammerhead Shark Raptor2150's Avatar
    Join Date
    Aug 2001
    Posts
    1,507

    Post

    Wow, is it really that difficult in c? I just wanted to check if an inputted variable of type float was actually a numeric value before I tried to do any calculations...

    ------------------
    Athlon XP 1800+ (OC pending)
    Alpha PAL8045
    Epox 8KHA+
    256mb Crucial pc2100 ddr
    Maxtor D740x 40 GB
    ATI Radeon8500 (300/300)
    Antec PP403x 400w

  5. #5
    Mako Shark dighn's Avatar
    Join Date
    Nov 2000
    Location
    Vancouver, BC Canada
    Posts
    3,171

    Post

    well u could alwasy write one yourself...

    this code will make sure the string is in the format -x.x where - . are optional and x can be any number of digits as long as it's at least 1 digit. this is just sort of a demo so there's lots of room for improvement

    Code:
    bool isNumeric(const char* s)
    {
      bool needDigit = true;
      bool gotDecimal = false;
    
      for(int i = 0; i < strlen(s);i++)
      {
        if(s[i] < '0' | | s[i] > '9')
        {
          if(s[i] == '-')
          {
    		if(i != 0)
              return false;
          }
          else if(s[i] == '.')
          {
    		if(needDigit)
              return false;
            if(gotDecimal)
              return false;
            needDigit = true;
            gotDecimal = true;
          }
          else
            return false;
        }
    	else
    		needDigit = false;
      }
      return !needDigit;
    }
    ------------------
    Keep it brief
    .

  6. #6
    Reef Shark
    Join Date
    Oct 2000
    Posts
    397

    Smile

    Originally posted by Raptor2150:
    I'm looking for something along the guidelines of IsNumeric() in VB for C++. Yeah, I'm a vb programmer
    The easiest way is to convert the string to a number using scanf:

    Code:
    char buffer[256] = "-10";
    int num;
    sscanf(buffer, "%d", &num);
    The trick is that scanf will return the number of arguments that it successfully assigned. Thus:

    Code:
    if( sscanf(buffer, "%d", &num) != 1 ) {
       /* error here */
    }

  7. #7
    Hammerhead Shark Raptor2150's Avatar
    Join Date
    Aug 2001
    Posts
    1,507

    Post

    Wow, is it really that complicated? I would think that there would be some sort of function for it. Anyways, thanks all.

    ------------------
    Athlon XP 1800+ (OC pending)
    Alpha PAL8045
    Epox 8KHA+
    256mb Crucial pc2100 ddr
    Maxtor D740x 40 GB
    ATI Radeon8500 (300/300)
    Antec PP403x 400w

  8. #8
    Hammerhead Shark
    Join Date
    Feb 2001
    Posts
    1,612

    Post

    Yep, C(++) doesn't always provide every little thing that people may want. But, that's why you make your own functions, then it is no longer complicated. ... as long as you have good comments.

  9. #9
    Catfish
    Join Date
    Aug 2001
    Posts
    177

    Post

    Uh, if(!isalpha(stuff)), or is that only PHP or something?

    ------------------
    EPoX 8KHA+
    Athlon XP 1900+
    512 MB DDR-SDRAM
    PNY GeForce3 TI 200
    EPoX 8KHA+
    Athlon XP 1900+
    512 MB DDR-SDRAM
    PNY GeForce3 TI 200

  10. #10
    Reef Shark
    Join Date
    Oct 2000
    Posts
    397

    Smile

    Originally posted by schnarf283:
    Uh, if(!isalpha(stuff)), or is that only PHP or something?
    Sure, you can use isdigit(...), however that doesn't really handle negative numbers properly. There are regular expression libraries, but these are cumbersome.

    so:

    Code:
    /* returns non-zero if true */
    int isAsciiInteger(char *nzstr)
    {
       int dummy;
       return(sscanf(nzstr, "%d", &dummy) == 1);
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •