CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Feb 2009
    Posts
    5

    String to int conversion

    Hi,

    I have read FAQS and used the methods given for string to integer conversion, that works fine but the scenario that i have tested is not being handled. i.e If there is string like "123AA" then all those methods provided in FAQS will convert the first 3 digits and discard the remaining string, and there will be no error. I need a function that should give error in the above case. Please reply if anyone can help me out.

    Thanks,
    Asif

  2. #2
    Join Date
    Mar 2001
    Posts
    2,529

    Re: String to int conversion

    Well you could always check each character to see if it is a numeric or not as in isdigit() or check if c >=0x30 && c < 0x39. Also you could just convert it back to a string, then compare the strings and if the strings are the same you know there were no alphas but if the strings are not the same, you know you had alphas!

    HTH,
    ahoodin
    To keep the plot moving, that's why.

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: String to int conversion

    strtod() and related functions allow you to get a pointer to the next bit of the string after the conversion. You can test if that points at a NULL terminator, or if there's more valid string remaining.

  4. #4
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: String to int conversion

    Not tested ... but I think it should work ...

    Code:
    template <typename T>
    bool Convert(const char * s , T & value)
    {
        std::stringstream ss(s);
    
        ss >> value;
    
        if (!ss) 
        {
            return false;
        }
        else
        {
            char c;
        
            ss >> c;
    
            return ss.fail();
        }
    }

  5. #5
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: String to int conversion

    Have a look at this thread, which handles the issue on a more generic level. The solution I use is calling s.ignore followed by s.gcount to see if all characters have been read/converted.
    More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf

    Premature optimization is the root of all evil --Donald E. Knuth


    Please read Information on posting before posting, especially the info on using [code] tags.

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: String to int conversion

    If you are using Boost, then consider using boost::lexical_cast. If not, I would propose a variant of Philip Nicoletti's suggestion (which is actually a variant of what boost::lexical_cast does):
    Code:
    template<typename T>
    bool convert(const std::string& s, T& value)
    {
        std::stringstream ss(s);
        return (ss >> value) && ss.eof();
    }
    EDIT:
    Of course, the assumption here is that you have already read the input into a string, e.g., using std::getline() or the overloaded operator>> for std::string.
    Last edited by laserlight; February 24th, 2009 at 10:33 AM.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  7. #7
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: String to int conversion

    Something like laserlight's solution was my first thought, but
    since leading whitespaces are allowed, I would think that
    trailing whitespaces should also be allowed.

    So, it all depends on what you want to check for.

  8. #8
    Join Date
    Feb 2009
    Posts
    5

    Re: String to int conversion

    Thank you all for your quick reply and support. I have tested "Philip Nicoletti" and "laserlight" version, both are working fine.

    Thanks,
    Asif

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured