How to validate a valid integer and floating number in VC++ CString

坚强是说给别人听的谎言 提交于 2021-02-07 18:17:41

问题


Can some one tell me a valid way to validate a number present in CString object as either a valid integer or floating number?


回答1:


Use _tcstol() and _tcstod():

bool IsValidInt(const CString& text, long& value)
{
    LPCTSTR ptr = (LPCTSTR) text;
    LPTSTR endptr;
    value = _tcstol(ptr, &endptr, 10);
    return (*ptr && endptr - ptr == text.GetLength());
}

bool IsValidFloat(const CString& text, double& value)
{
    LPCTSTR ptr = (LPCTSTR) text;
    LPTSTR endptr;
    value = _tcstod(ptr, &endptr);
    return (*ptr && endptr - ptr == text.GetLength());
}

EDIT: Modified the code to follow the excellent suggestions provided in the comments.



来源:https://stackoverflow.com/questions/4030337/how-to-validate-a-valid-integer-and-floating-number-in-vc-cstring

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!