CTEC1335/2008F Using C Standard Library Functions to Convert String Input ---------------------------------------------------------- #include int atoi( const char * s ) ; http://www.cplusplus.com/reference/clibrary/cstdlib/atoi.html double atof( const char * s ) ; http://www.cplusplus.com/reference/clibrary/cstdlib/atof.html Limitations: Both functions return a zero if the a numeric value cannot be converted, which makes it difficult to determine if an actual zero value was converted. #include int sscanf( const char * s, const char * format, ... ) ; http://www.cplusplus.com/reference/clibrary/cstdio/sscanf.html Limitations: Large values will either be "wrapped around" (ints) or converted to an "infinite" value. Small values will be converted to zero. To convert a C++ string variable to a C-style string, use the const char * c_str( ) const ; method. http://www.cplusplus.com/reference/string/string/c_str.html For example, string s ; . . . const char * cs = s.c_str( ) ; Example programs: atof.cpp Demonstrate using atof( ) for double input atoi.cpp Demonstrate using atoi( ) for int input sscanfdouble.cpp Demonstrate using sscanf( ) for double input sscanfint.cpp Demonstrate using sscanf( ) for int input stringUtil.cpp String utility library, for use with parsers stringUtil.h String utility library header file stripleading.cpp Test program for stripleadingspace( ) function striptailing.cpp Test program for striptailingspace( ) function parseInt.cpp An integer parser For the first four programs atox.cpp and sscanfxxx.cpp, compile with: cl /EHsc ____.cpp For the stringUtil.cpp, compile with: cl /I. /EHsc /C stringUtil.cpp For the last three programs (stripxxx.cpp and parseInt.cpp), compile with: cl /I. /EHsc _____.cpp stringUtil.obj