Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. how to do an isnumeric

how to do an isnumeric

Scheduled Pinned Locked Moved C / C++ / MFC
tutorial
8 Posts 6 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    neodeaths
    wrote on last edited by
    #1

    i am trying to do something like this if(abc=numeric) cout << "abc is numeric"; obviyesly this does not work can anyone giv me the code for the is numeric function thx^^

    R C J J 4 Replies Last reply
    0
    • N neodeaths

      i am trying to do something like this if(abc=numeric) cout << "abc is numeric"; obviyesly this does not work can anyone giv me the code for the is numeric function thx^^

      R Offline
      R Offline
      Ryan McDermott
      wrote on last edited by
      #2

      Well are you trying to test if its equal, because if you are then you meant to do if(abc==numeric) cout << "abc is numeric"; edit: sorry i dont think i read that right, maybe you might try googling the "isnum" function.

      1 Reply Last reply
      0
      • N neodeaths

        i am trying to do something like this if(abc=numeric) cout << "abc is numeric"; obviyesly this does not work can anyone giv me the code for the is numeric function thx^^

        C Offline
        C Offline
        Christian Graus
        wrote on last edited by
        #3

        isalphanum && !isalpha. You need to do it char by char, if you have a std:string you can use a function object to do this. Christian Graus - Microsoft MVP - C++

        B 1 Reply Last reply
        0
        • N neodeaths

          i am trying to do something like this if(abc=numeric) cout << "abc is numeric"; obviyesly this does not work can anyone giv me the code for the is numeric function thx^^

          J Offline
          J Offline
          Jack Puppy
          wrote on last edited by
          #4

          I found this in one of my old libraries. Ignore the CHECK, it's an internal macro.

          /// Determines if a string is numeric
          /// @param lpszText Text to determine numericy of
          /// @param bSigned Determines if the function tests against signed or unsigned
          /// numbers
          /// @param nBase Numeric base to test against
          /// @return True if the string is numeric, false if not
          BOOL AfxIsNumeric(LPCTSTR lpszText, BOOL bSigned, int nBase)
          {
          CHECK(lpszText != NULL, FALSE);

          // Convert the text to a number using the appropriate sign oriented
          // function
          LPTSTR lpszEndPtr = NULL;
          
          if (bSigned)
              \_tcstoi64(lpszText, &lpszEndPtr, nBase);
          else
              \_tcstoui64(lpszText, &lpszEndPtr, nBase);        
          
          // If the end pointer points to the null terminator, and it doesn't point to
          // the start of the text, (which would occur on an empty string) it's a
          // valid number
          return ((lpszEndPtr != lpszText) && (\*lpszEndPtr == \_T('\\0')));
          

          }

          :suss: Pssst. You see that little light on your monitor? That's actually a government installed spy camera. Smile and wave to big brother!
          Painted on the side of a dog trainer's van: SIT HAPPENS

          1 Reply Last reply
          0
          • N neodeaths

            i am trying to do something like this if(abc=numeric) cout << "abc is numeric"; obviyesly this does not work can anyone giv me the code for the is numeric function thx^^

            J Offline
            J Offline
            Jose Lamas Rios
            wrote on last edited by
            #5

            Try something like this:

            #include <ctype.h>
             
            bool IsNumeric(const char* p)
            {
               int len = strlen(p);
               for (int i = 0; i < len; i++)
               {
                  if (!isdigit(p[i]))
                     return false;
               }
             
               return true;
            }

            Note: I didn't compiled it, so it may contain some error :) -- jlr http://jlamas.blogspot.com/[^]

            1 Reply Last reply
            0
            • C Christian Graus

              isalphanum && !isalpha. You need to do it char by char, if you have a std:string you can use a function object to do this. Christian Graus - Microsoft MVP - C++

              B Offline
              B Offline
              Bob Stanneveld
              wrote on last edited by
              #6

              Christian Graus wrote: isalphanum && !isalpha isdigit looks a little bit more nice. Behind every great black man...             ... is the police. - Conspiracy brother Blog[^]

              C 1 Reply Last reply
              0
              • B Bob Stanneveld

                Christian Graus wrote: isalphanum && !isalpha isdigit looks a little bit more nice. Behind every great black man...             ... is the police. - Conspiracy brother Blog[^]

                C Offline
                C Offline
                Christian Graus
                wrote on last edited by
                #7

                Yeah, someone else answered that. I'd never seen that - I am self taught, and when I looked for isnumeric, I obviously didn't spot isdigit, and I kept using that method ever since. Live and learn, I guess.... Christian Graus - Microsoft MVP - C++

                B 1 Reply Last reply
                0
                • C Christian Graus

                  Yeah, someone else answered that. I'd never seen that - I am self taught, and when I looked for isnumeric, I obviously didn't spot isdigit, and I kept using that method ever since. Live and learn, I guess.... Christian Graus - Microsoft MVP - C++

                  B Offline
                  B Offline
                  Bob Stanneveld
                  wrote on last edited by
                  #8

                  I'm also self taught. I came across that function a few weeks ago when I also needed to chech if a string was numeric or not. It's not the ideal solution since it will return false on hexa decimal numbers, but it's a start.. :) Christian Graus wrote: Live and learn, I guess.... We developers only stop learning when we die.. If you stop learning, you'll soon be obsolete. Behind every great black man...             ... is the police. - Conspiracy brother Blog[^]

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups