checking isnumaric for a string variable

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • reshmaah
    New Member
    • May 2007
    • 10

    checking isnumaric for a string variable

    I want to check if the
    char a[] ="123",
    contents of a is numaric value or not
    plz reply soon
  • ilikepython
    Recognized Expert Contributor
    • Feb 2007
    • 844

    #2
    Originally posted by reshmaah
    I want to check if the
    char a[] ="123",
    contents of a is numaric value or not
    plz reply soon
    You can use the isdigit() function in the <cctype> header file.

    Comment

    • reshmaah
      New Member
      • May 2007
      • 10

      #3
      Originally posted by ilikepython
      You can use the isdigit() function in the <cctype> header file.
      but isdigit() works for character , i want to check is string is number

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        You can write your own function. Check each character with isdigit() - if a test ever fails, then you can return false - it is not a number. If you have finished checking the string and have not returned false, then there was no non-numeric character, and the string is a number - you can return true.

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by Ganon11
          You can write your own function. Check each character with isdigit() - if a test ever fails, then you can return false - it is not a number. If you have finished checking the string and have not returned false, then there was no non-numeric character, and the string is a number - you can return true.
          That scenario sort of works but what about strings such as:
          "12345678901234 567890123456789 012345678901234 567890123456789 0"?
          It definitely is an integer in the theoretical sense of the meaning but in practice
          it doesn't fit in a normal int nor long.

          kind regards,

          Jos (<-- nitpicker ;-)

          Comment

          • Ganon11
            Recognized Expert Specialist
            • Oct 2006
            • 3651

            #6
            Originally posted by JosAH
            That scenario sort of works but what about strings such as:
            "12345678901234 567890123456789 012345678901234 567890123456789 0"?
            It definitely is an integer in the theoretical sense of the meaning but in practice
            it doesn't fit in a normal int nor long.

            kind regards,

            Jos (<-- nitpicker ;-)
            True...then again, in this case, would any solution work? No matter what method is used to determine that the string is, in fact, a number, the number remains too large to be represented.

            Don't worry about being a nitpicker - picking at nits is how a coder gets better.

            Comment

            • Savage
              Recognized Expert Top Contributor
              • Feb 2007
              • 1759

              #7
              Originally posted by JosAH
              That scenario sort of works but what about strings such as:
              "12345678901234 567890123456789 012345678901234 567890123456789 0"?
              It definitely is an integer in the theoretical sense of the meaning but in practice
              it doesn't fit in a normal int nor long.

              kind regards,

              Jos (<-- nitpicker ;-)
              He can create, in his function, that longest number can have up to 6 chars(-32768 for int) and after entering 5th(or 6th if there is minus) that it automaticly break from input.Also he will need to include '-' sign as a option when inputing.

              Savage

              Comment

              • reshmaah
                New Member
                • May 2007
                • 10

                #8
                Originally posted by Savage
                He can create, in his function, that longest number can have up to 6 chars(-32768 for int) and after entering 5th(or 6th if there is minus) that it automaticly break from input.Also he will need to include '-' sign as a option when inputing.

                Savage
                thanks problem is solved

                Comment

                • Savage
                  Recognized Expert Top Contributor
                  • Feb 2007
                  • 1759

                  #9
                  Originally posted by reshmaah
                  thanks problem is solved
                  No prob.

                  I'm more than happy to help u!

                  Savage

                  Comment

                  Working...