isdigit 在输入 £ 和 ¬ 时引发调试断言
isdigit raises a debug assertion when entering £ and ¬
下面的代码适用于我输入的每个字符,除了 £
或 ¬
。
为什么我得到 "debug assertion fail"?
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main() {
string input;
while (1) {
cout << "Input number: ";
getline(cin, input);
if (!isdigit(input[0]))
cout << "not a digit\n";
}
}
The behavior of isdigit and _isdigit_l is undefined if c is not EOF or in the range 0 through 0xFF, inclusive. When a debug CRT library is used and c is not one of these values, the functions raise an assertion.
(我猜微软是因为关于 "error window" 的评论,但其他实现的文档对参数值设置了相同的限制。)
编辑:正如 Deduplicator 所观察到的,错误可能是由于默认 char
在此平台上签名,因此您传递的是负值(与 EOF
不同)。 std::string
用的是char
,不是宽字符,所以我原来的结论不可能是正确的。
The C++ compiler treats variables of type char, signed char, and unsigned char as having different types. Variables of type char are promoted to int as if they are type signed char by default, unless the /J compilation option is used. In this case they are treated as type unsigned char and are promoted to int without sign extension.
The behavior of isdigit
and _isdigit_l
is undefined if c
is not EOF
or in the range 0
through 0xFF
, inclusive. When a debug CRT library is used and c
is not one of these values, the functions raise an assertion.
所以 char
默认是 signed
,这意味着这两个字符不是 ASCII,它们在你的 ANSI 字符集中是负数,因此你得到了断言。
下面的代码适用于我输入的每个字符,除了 £
或 ¬
。
为什么我得到 "debug assertion fail"?
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main() {
string input;
while (1) {
cout << "Input number: ";
getline(cin, input);
if (!isdigit(input[0]))
cout << "not a digit\n";
}
}
The behavior of isdigit and _isdigit_l is undefined if c is not EOF or in the range 0 through 0xFF, inclusive. When a debug CRT library is used and c is not one of these values, the functions raise an assertion.
(我猜微软是因为关于 "error window" 的评论,但其他实现的文档对参数值设置了相同的限制。)
编辑:正如 Deduplicator 所观察到的,错误可能是由于默认 char
在此平台上签名,因此您传递的是负值(与 EOF
不同)。 std::string
用的是char
,不是宽字符,所以我原来的结论不可能是正确的。
The C++ compiler treats variables of type char, signed char, and unsigned char as having different types. Variables of type char are promoted to int as if they are type signed char by default, unless the /J compilation option is used. In this case they are treated as type unsigned char and are promoted to int without sign extension.
The behavior of
isdigit
and_isdigit_l
is undefined ifc
is notEOF
or in the range0
through0xFF
, inclusive. When a debug CRT library is used andc
is not one of these values, the functions raise an assertion.
所以 char
默认是 signed
,这意味着这两个字符不是 ASCII,它们在你的 ANSI 字符集中是负数,因此你得到了断言。