Checking if a var is numeric?

This is a discussion on Checking if a var is numeric? within the C++ Programming forums, part of the General Programming Boards category; How can you check if for example the x[30] char has numbers in it, without using any specific function if ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    62

    Checking if a var is numeric?

    How can you check if for example the x[30] char has numbers in it, without using any specific function if possible? And the same with ints, how do you check if it's a number?

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,455
    Best way of doing it is using std::isdigit. It takes a single character, so if you want to check the entire string, you'll have to loop through it. It's the recommended way for portability.
    And integers are integers, not strings. In fact, strings are characters which are integers, so it's impossible to separate those.
    Is there a specific situation you're thinking of?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,167
    Code:
    bool has_numbers = false;
    for (int i = 0; i < 30; ++i) {
         if (x[i] >= '0' && x[i] <= '9') {
              has_numbers = true;
              break;
         }
    }
    This assumes the encoding scheme of ASCII, which I think is an assumption that is safe to make.

    But of course, std::isdigit is a better solution, if you don't mind using the standard library. (why not?)

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    This assumes the encoding scheme of ASCII,
    It doesn't. It merely assumes that the digits are sequential in the encoding, which is a requirement of the standard. And it assumes that digits are represented by a single byte, which is not a requirement AFAIK, but you'll be hard-pressed to find an implementation where it is not the case.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,167
    digits are sequential in the encoding
    and that '0' is the first of that sequence, and '9' is the last.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    2.2/3 says:
    "In both the source and execution basic character sets, the value of each character after 0 in the above list of decimal digits shall be one greater than the value of the previous."
    where above list is:
    0 1 2 3 4 5 6 7 8 9

    So yeah, that too.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User
    Join Date
    May 2008
    Posts
    62
    Quote Originally Posted by Elysia View Post
    Is there a specific situation you're thinking of?
    For example in PHP, you protect the script by requesting a numeric check whenever it's only numbers that are adequate. Is it "the same" in C++?

  8. #8
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,167
    if you mean checking for user input, I think the convention is to try to read into an integer, and then check for the fail flag of the stream.

    Code:
    int should_be_a_number;
    cin >> should_be_a_number;
    if (cin.fail()) {
         ... // the user didn't input a number
    }

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by cyberfish View Post
    This assumes the encoding scheme of ASCII
    It's a-z and A-Z that assumes ASCII, not numbers.

  10. #10
    Registered User
    Join Date
    May 2008
    Posts
    62
    Quote Originally Posted by cyberfish View Post
    if you mean checking for user input, I think the convention is to try to read into an integer, and then check for the fail flag of the stream.

    Code:
    int should_be_a_number;
    cin >> should_be_a_number;
    if (cin.fail()) {
         ... // the user didn't input a number
    }
    Can you go inte greater detail on what "cin." does?

  11. #11
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,167

  12. #12
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Glauber View Post
    Can you go inte greater detail on what "cin." does?
    If you need to ask that question, you definitely need to get a C++ book to learn from.

  13. #13
    Registered User
    Join Date
    May 2008
    Posts
    62
    Quote Originally Posted by cpjust View Post
    If you need to ask that question, you definitely need to get a C++ book to learn from.
    Theres a reason for why I choose the forum. You mind elaborating on that really quick?

  14. #14
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    We're not your personal teaching assistants.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  15. #15
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,455
    Cin is a global object that is used to read input from the console screen, from the user.
    But you should probably get a book, because they explain all this in great detail.
    There's a good thread that has many suggestions for books.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

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

Similar Threads

  1. Need help assignment
    By 6kaine9 in forum C Programming
    Replies: 26
    Last Post: 10-19-2008, 08:51 PM
  2. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  3. Problems about gcc installation
    By kevin_cat in forum Linux Programming
    Replies: 4
    Last Post: 08-09-2005, 09:05 AM
  4. server question
    By xddxogm3 in forum Tech Board
    Replies: 24
    Last Post: 01-21-2004, 12:12 AM
  5. Vigenere Decipher/Encipher
    By Xander in forum C++ Programming
    Replies: 5
    Last Post: 02-15-2002, 08:24 AM

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