How to detect if atof or _wtof failes to convert the string to double? But not by trying to check if the result is different form 0.0 because my input can be 0.0. Thanks!
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Don’t use
atof. Instead, usestrtod, from<cstdlib>, and also checkerrnofrom<cerrno>:The pointer
epoints one past the last consumed character. You can also checke == mystrto see if any characters got consumed.There’s also
std::wcstodfor working withwchar_t-strings, from<cwstring>.In C++11 you also have
std::to_string/std::to_wstring, from<string>, but I believe that throws an exception if the conversion fails, which may not be a desirable failure mode when dealing with external data.