Error Check

This is a discussion on Error Check within the C++ Programming forums, part of the General Programming Boards category; Hi guys, How do I say if something "is a number" ie) PSEUDO CODE IF X is a number THEN ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    224

    Error Check

    Hi guys,

    How do I say if something "is a number"

    ie)

    PSEUDO CODE
    IF X is a number THEN
    .....
    ELSE

    etc.


    Thanks

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You can't do that in C & C++.

    You can either check if the input was done correctly, e.g. in C++ you would do:
    Code:
    int x;
    if (cin >> x)
    {
       .. x is a number... 
    }
    else
    {
        .. input was not valid, either x is not a number, or something else 
           didn't allow a valid value to be read - e.g. end of file. 
    }
    Or you read the data in as a string (which will accept ANYTHING) using for example getline(cin, str), and then try to convert the input to a number.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    224
    Thanks

    So, with the string. How am I going to check that it is not a value?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by strokebow View Post
    Thanks

    So, with the string. How am I going to check that it is not a value?
    There are several variants (and you should of course still check that the input itself was OK - otherwise you may end up processing the old data from last successful operation, potentially FOREVER, if there is nothing else stopping the program).

    So, the most C++ style thing is to use stringstream, which is like cin/cout and such, except it does the "in" and "out" in form of a string. So something like this:
    Code:
    #include <sstream>
    
    
    ... in some function ... 
    std::string str = "1234";
    std::stringstream ss;
    int x;
    ss << str;
    if (ss >> x)
       ... str was a number.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,762
    If you want to check that there are no non-numeric characters following the number, modify matsp's example to:
    Code:
    std::stringstream ss(str);
    int x;
    if (ss >> x && ss.eof())
        // ... str was a number.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,629
    There is also boost::lexical_cast. But it throws in case of error:
    Code:
    try
    {
        int x = boost::lexical_cast<int>(str);
    }
    catch(const boost::bad_lexical_cast&)
    {
        // str is not an integer
    }
    boost::lexical_cast can pretty much convert anything to anything. It can also be extended with custom data types if the custom data types provide the correct interface (read: they are done correctly).
    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.

Popular pages Recent additions subscribe to a feed

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