开发者

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

Can some one tell me a valid way to validate a number present in CString object as either a val开发者_运维技巧id integer or floating number?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜