Function Libraries?

Feb 11, 2013 at 8:00pm
All, does anyone know of any free (or even not free) collection of common functions such as converting strings to numbers (with error checking, etc.) and stuff like that?

In other words, things that will make a real-world interface without causing a program to crash?

Thanks in advance.

- Bill
Feb 11, 2013 at 8:07pm
closed account (3qX21hU5)
Boost is probably the best one out there in my opinion. It has many useful things in it that can be used for a variety of things like your string to int question
Feb 11, 2013 at 8:09pm
I believe the common C++ libraries includes this functionality. Here is an example:
http://www.cplusplus.com/reference/cstdlib/atoi/
Feb 11, 2013 at 8:22pm

Thanks very much for the fast replies. I took a look at the atoi function in the standard libraries but it has limitations. It returns "0" for a failure but zero is a valid number as well. It also returns a number up to the first invalid number in string format. For example '56xxt' will return 56.

I will try and google "boost" and see if that works for me.

I'm looking for something that is bulletproof.

Thanks all,
- Bill
Feb 11, 2013 at 8:27pm
closed account (3qX21hU5)
Something like this?

1
2
3
4
5
6
7
8
9
10
11
#include <boost/lexical_cast.hpp>

try 
{
    int x = boost::lexical_cast<int>("546");
} 

catch(boost::bad_lexical_cast const&) 
{
    std::cout << "Error: string is not valid" << std::endl;
}


http://www.boost.org/doc/libs/1_53_0/doc/html/boost_lexical_cast.html
Last edited on Feb 11, 2013 at 8:33pm
Topic archived. No new replies allowed.