how to check if the string is a number

This is a discussion on how to check if the string is a number within the C++ Programming forums, part of the General Programming Boards category; my program will read in a string from user input, how do i check if it is a number?...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    7

    how to check if the string is a number

    my program will read in a string from user input, how do i check if it is a number?

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    I think you can answer your own question by asking yourself: What is a number?

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    7
    i tried using isdigit, but it is not working. Or maybe anyone can tell me how to use isdigit() to check if the string is a number?

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Here's a pretty good reference for you to answer questions like this on your own.

    http://www.cppreference.com/

    Now, they still use the older header files like ctype.h instead of the newer, prefered headers like cctype, but that's a minor consideration.

    For now, you need to include either the ctype.h or the cctype header file in your program in order to use isdigit(). Then one syntax you could use couuld be something like this:

    Code:
    char ch = '1';
    if(isdigit(ch))
      cout << "ch is a digit";
    You're only born perfect.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,663
    > i tried using isdigit, but it is not working
    It's usually a good idea to post your attempt rather than just complaining "it didn't work".
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    If you are reading from cin, the simplest method is to use cin's built-in number checking. The easiest way to do that is to check the return value of cin:
    Code:
    if (cin >> myint)
        // myint is a number
    Sometimes people would prefer a loop that prompts the user to try again:
    Code:
    while (!(cin >> myint))
    {
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        // re-prompt.
    }
    Finally, sometimes people want to also make sure that a number followed by text doesn't count either (e.g. 42g is not a number):
    Code:
    while (!(cin >> myint) || cin.get() != '\n')
    {
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        // re-prompt.
    }
    At this point it works pretty well, although there are still other features that can be added. At some point it is overkill, thuogh, so the above should work fine for checking if the input is a number.

    If you have a string, use a stringstream instead of cin, and of course get rid of the loop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  3. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  4. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  5. how can i prove a string does not contain a number ?
    By blue_gene in forum C++ Programming
    Replies: 7
    Last Post: 04-07-2004, 09:35 PM

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