c++ - 如何在 VC++ CString 中验证有效的整数和 float

标签 c++ mfc

谁能告诉我一种有效的方法来验证 CString 对象中存在的数字是有效整数还是 float ?

最佳答案

使用_tcstol()_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());
}

编辑:修改代码以遵循评论中提供的优秀建议。

关于c++ - 如何在 VC++ CString 中验证有效的整数和 float ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4030337/

相关文章:

c++ - 未格式化的 i/o 进出内存

C++ 指针赋值语句似乎不起作用/做任何事情

c++ - C++ 字符串的算术运算

c++ - 从 MFC 应用程序连接到 SQL Server Compact Edition (.sdf)

visual-studio-2008 - 奇怪的 MFC/VC++ 链接器错误(std::list<CRect> 已定义)

c++ - C++代码最好的prettyprint选项是什么?

c++ - 在 C++0x 中未调用移动构造函数

MFC - 更改静态文本控件的文本颜色

visual-c++ - 将文本框绑定(bind)到 DDX 中的控制与值

c++ - 如何在 MFC 功能区应用程序中使用自动完成编辑控件