gpt4 book ai didi

c++ - isdigit 在输入 £ 和 ¬ 时引发调试断言

转载 作者:行者123 更新时间:2023-11-30 01:15:41 25 4
gpt4 key购买 nike

下面的代码适用于我输入的每个字符,£¬ 除外。

为什么我会收到“调试断言失败”?

#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 microsoft docs say :

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.

And they also say :

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 字符集中是负数,因此您得到了断言。

关于c++ - isdigit 在输入 £ 和 ¬ 时引发调试断言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28077229/

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