gpt4 book ai didi

c++ - 验证像 "-2-2"或 "--22"或 "2--2"这样的 CString 是无效的

转载 作者:行者123 更新时间:2023-11-28 06:29:00 24 4
gpt4 key购买 nike

在 C++ 中如何验证像“-2-2”或“--22”或“2--2”这样的 CString 不是有效整数。

我尝试了以下代码:

// CString input in the edit box that needs to be verified
CString input_to_editbox
// verify its a number or not
if (input_to_editbox.SpanIncluding(L"-0123456789") == input_to_editbox)
//numeric
AfxMessageBox(L"it is an integer");
else
//Non numeric
AfxMessageBox(L"Not an integer");

但我无法验证“-2-2”或“--22”或“2--2”是非法和错误的整数。

最佳答案

我在使用对输入不是很严格的 DDX_ 函数时遇到了同样的问题。我已经用 scanf(或更好的 _stscanf_s)解决了这个问题。

int nValue;
unsigned charsRead = 0;
const int fieldsRead = _stscanf_s(str, _T("%d%n"), &nValue, &charsRead);
if(fieldsRead == 1 && charsRead == _tcslen(str)) {
// We have read all fields and we have read the complete string,
// so str contains a valid pattern.
}

如果您想读取其他类型(例如“%u%n”)或多个值(例如“rate is: %d/%d Mbits/s%n”),您可以轻松调整该方法。只是不要忘记将 fieldsRead 与正确的值进行比较。

关于c++ - 验证像 "-2-2"或 "--22"或 "2--2"这样的 CString 是无效的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27984209/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com