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 check if char* is a number

How to check if char* is a number

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
9 Posts 5 Posters 0 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.
  • P Offline
    P Offline
    piul
    wrote on last edited by
    #1

    I've got a function that returns a char* type and I need to check if this contains a number or some other type of characters. How could I do this? I've been checking the articles/messages but I couldn't find an example... Thanks!

    C 1 Reply Last reply
    0
    • P piul

      I've got a function that returns a char* type and I need to check if this contains a number or some other type of characters. How could I do this? I've been checking the articles/messages but I couldn't find an example... Thanks!

      C Offline
      C Offline
      CPallini
      wrote on last edited by
      #2

      You may use the strtol[^] function. :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
      [My articles]

      P 1 Reply Last reply
      0
      • C CPallini

        You may use the strtol[^] function. :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        P Offline
        P Offline
        piul
        wrote on last edited by
        #3

        oops, I removed the message before seeing a reply was given. I found a way anyhow, checking the ascii code

        const char \* pcField = sFileNumber.c\_str();
        int a = int(\*pcField);
        if (a >= 48 && a <= 57)	
        	//...
        
        M F 2 Replies Last reply
        0
        • P piul

          oops, I removed the message before seeing a reply was given. I found a way anyhow, checking the ascii code

          const char \* pcField = sFileNumber.c\_str();
          int a = int(\*pcField);
          if (a >= 48 && a <= 57)	
          	//...
          
          M Offline
          M Offline
          Maximilien
          wrote on last edited by
          #4

          bad, bad programming. :^)

          Watched code never compiles.

          P 1 Reply Last reply
          0
          • P piul

            oops, I removed the message before seeing a reply was given. I found a way anyhow, checking the ascii code

            const char \* pcField = sFileNumber.c\_str();
            int a = int(\*pcField);
            if (a >= 48 && a <= 57)	
            	//...
            
            F Offline
            F Offline
            federico strati
            wrote on last edited by
            #5

            better to use "isdigit" and related similar functions ("isalpha", "isspace", etc...)

            1 Reply Last reply
            0
            • M Maximilien

              bad, bad programming. :^)

              Watched code never compiles.

              P Offline
              P Offline
              piul
              wrote on last edited by
              #6

              ok, ok. I'll use isdigit(). I know my solution is not good looking, but, can I ask why is it bad programming?

              C 1 Reply Last reply
              0
              • P piul

                ok, ok. I'll use isdigit(). I know my solution is not good looking, but, can I ask why is it bad programming?

                C Offline
                C Offline
                CPallini
                wrote on last edited by
                #7

                Maybe not that bad. Ugly, neverthless. Anyway you're just checking the first character of the string. Is this OK (i.e. the string will always contain at most 1 digit)? :)

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                [My articles]

                P 1 Reply Last reply
                0
                • C CPallini

                  Maybe not that bad. Ugly, neverthless. Anyway you're just checking the first character of the string. Is this OK (i.e. the string will always contain at most 1 digit)? :)

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                  This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                  [My articles]

                  P Offline
                  P Offline
                  piul
                  wrote on last edited by
                  #8

                  err... no, it may contain more than one digit. I see what you mean, it could be a character-string started by a number... Highly improbable in my application, but I should check it anyway, shouldn't I?

                  A 1 Reply Last reply
                  0
                  • P piul

                    err... no, it may contain more than one digit. I see what you mean, it could be a character-string started by a number... Highly improbable in my application, but I should check it anyway, shouldn't I?

                    A Offline
                    A Offline
                    Alain Rist
                    wrote on last edited by
                    #9

                    Simplest:

                    bool IsDigits(const std::string& str)
                    {
                    return str.empty() ? false : str.find_first_not_of("0123456789") == str.npos;
                    }

                    More efficient:

                    bool IsDigits(const std::string& str)
                    {
                    return str.empty() ? false : std::find_if_not(str.begin(), str.end(), isdigit) == str.end();
                    }

                    cheers, AR Edit: changed function name to IsDigits and added second version. ReEdit: added empty string handling.

                    When the wise (person) points at the moon the fool looks at the finger (Chinese proverb)

                    modified on Thursday, November 4, 2010 1:40 PM

                    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