Thread: Error Check

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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