Library Function to verify Int

This is a discussion on Library Function to verify Int within the C++ Programming forums, part of the General Programming Boards category; I believe this does everything ZuK's version does, just a little more succinctly: Code: bool is_int(const std::string& s) { int ...

  1. #16
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    I believe this does everything ZuK's version does, just a little more succinctly:
    Code:
    bool is_int(const std::string& s)
    {
        int i;
        std::istringstream istr(s);
        if (!(istr >> i))
            return false;
    
        return istr.rdbuf()->in_avail() == 0;
    }

  2. #17
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Code:
    bool isint( const string& s ) {
    In C at least, a function name starting with "is" and followed by a lowercase letter is reserved (for future use in <ctype>). Name it is_int instead or something.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #18
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    None of the versions work with hex (as currently implemented). I believe the stringstream version would require using an io manipulator, which would require a lot more code since you don't know which manipulator to use.

  4. #19
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by Stoned_Coder
    yes it is but its in base 16. its a valid int. oxff is 255 for example and can be passed to a func requiring an int.
    you can't pass "0xfc" to atoi() and expect it to return the correct integer. Just ain't going to happen. Why stop at hex, why not pass octal too? That function isn't supposed to convert numbers from one base to another. Use the "KISS" (Keep It Simple Stupid) principal here.

  5. #20
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    atoi expects const char* not int

    Yes why not do octal too. Anything the language sees as a valid int.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  6. #21
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    >> atoi expects const char* not int
    "0xfc" is a const char*.

    Even the C++ input mechanism doesn't deal with the issue of hex or octal. Your code has to specify that you want to read in the input in hexadecimal format from the user. Otherwise, using cin >> an_int will fail if the user types 0xff. Since the OP wants to know about validating user input, I think adding checks for other bases is probably not necessary.

  7. #22
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by Stoned_Coder
    atoi expects const char* not int
    I didn't mean pass it an int, a string
    atoi("0xff");

    Quote Originally Posted by Stoned_Coder
    Yes why not do octal too. Anything the language sees as a valid int.
    Of course there are standard library functions that will convert strings from one base to another, but it requires another parameter to tell it what base to use. I don't see that as the op's intention.

    Dave: your solution is really great. I didn't see it before posting my last response.
    Last edited by Ancient Dragon; 09-29-2005 at 12:50 PM.

  8. #23
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    the op asked for a way to make sure input was a valid int. well input in hex or octal is also a valid int.
    A func that takes an int param(not const char* as atoi does, i never said that would work), can be passed a hex or octal number and thats fine because it is an int.
    So I believe any function designed to check an ints validity has to take octal and hex into account. Dont you?
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  9. #24
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    I don't agree. The function takes a string, not an int. When you pass a hex (or oct), the compiler converts it to internal int format, which means you aren't passing a hex or oct at all. The receiving function never sees what you typed in the source code.
    Code:
    is_int(0xff);
    The above won't work because is_int() expects the parameter to be a string. What is the purpose of overloading is_int() to accept an int instead of a string? An int is obviously a valid int or it wouldn't be called an int. How can an int possibly be an invalid int?
    Last edited by Ancient Dragon; 09-29-2005 at 01:06 PM.

  10. #25
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    I don't thin kit is really necessary to check for hex and octal values, but if that is what you want, I think this is appropriate:
    Code:
    #include <sstream>
    #include <string>
    
    bool is_int(const std::string& s)
    {
        int i;
        std::istringstream istr(s);
        istr.unsetf(std::ios::dec);
        if (!(istr >> i))
            return false;
    
        return istr.rdbuf()->in_avail() == 0;
    }
    And a test program:
    Code:
    #include <iostream>
    
    int main()
    {
        if (is_int("1234"))
            std::cout << "1234 is int." << std::endl;
        else
            std::cout << "1234 is not int." << std::endl;
    
        if (is_int("12ab34"))
            std::cout << "12ab34 is int." << std::endl;
        else
            std::cout << "12ab34 is not int." << std::endl;
    
        if (is_int("2147483648"))
            std::cout << "2147483648 is int." << std::endl;
        else
            std::cout << "2147483648 is not int." << std::endl;
    
        if (is_int("2147483647"))
            std::cout << "2147483647 is int." << std::endl;
        else
            std::cout << "2147483647 is not int." << std::endl;
    
        if (is_int("-1234"))
            std::cout << "-1234 is int." << std::endl;
        else
            std::cout << "-1234 is not int." << std::endl;
    
        if (is_int("0x12ab34"))
            std::cout << "0x12ab34 is int." << std::endl;
        else
            std::cout << "0x12ab34 is not int." << std::endl;
    
        if (is_int("02222222222"))
            std::cout << "02222222222 is int." << std::endl;
        else
            std::cout << "02222222222 is not int." << std::endl;
    
        if (is_int("6."))
            std::cout << "6. is int." << std::endl;
        else
            std::cout << "6. is not int." << std::endl;
    }

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

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 04:26 PM

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