How to check valid integer and floating point number in VC ++ CString

Can someone tell me the correct way to check the number present in a CString object as a real integer or a floating number?

+3
source share
1 answer

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: Changed the code to follow the great suggestions in the comments.

+6
source

Source: https://habr.com/ru/post/1771636/


All Articles