I have numeric input which I must convert to strings and validate whilst doing the conversion. I do not want to mix C functions like strtol in to my c++ code, so I'm trying to implement a C++ method using examples found in google but none of them work :(
I have this test program to show my misunderstanding of what is going on. It also tests a second problem where I am trying to reset a istringstream but need to call the clear() member after the reset, which as I misunderstand things is not supposed to be required. #include <sstream> #include <iostream> long testLong; std::string testString; std::istringstream iss; iss.exceptions(std::istringstream::eofbit | std::istringstream::failbit | std::istringstream::badbit); iss.str(argv[1]); try // supposedly this will throw exception if conversion is non- numeric { iss >> testLong; } catch(std::istringstream::failure& e) { std::cout << "caught a exception geezer! [" << e.what() << "]" << std::endl; } if (!iss) // supposedly this is the way to check for invalid numeric input { std::cout << "conversion was illegal geezer!" << std::endl; } std::cout << std::endl << std::endl << "first string [" << testLong << "]" << std::endl; iss.str("second string"); iss.clear(); // should not have to do this but it does not reset the string stream without it!!! iss >> testString; std::cout << std::endl << std::endl << "second string [" << testString << "]" << std::endl; return(EXIT_SUCCESS); } Here is the output of 3 runs, the first with invalid alpha input, the second with invalid alpha/numeric input and the third with valid numeric input. // test run with invalid alpha input /test this-is-text caught a exception geezer! [basic_ios::clear] conversion was illegal geezer! first string [0] second string [second] // test run with invalid mixed alpha/numeric input ./test 123pep89 first string [123] second string [second] // test run with valid numeric input ./test 12389 caught a exception geezer! [basic_ios::clear] first string [12389] second string [second] So it appears that none of the methods used are capable of doing what I require, even though there are hundreds of entries in google that state this is the way to do? What are the "caught a exception geezer! [basic_ios::clear]" exceptions about? So how can I do what I want to do, which is simply to convert and validate a numeric input value to a string value? TIA, Pep _______________________________________________ help-gplusplus mailing list help-gplusplus@gnu.org http://lists.gnu.org/mailman/listinfo/help-gplusplus