gpt4 book ai didi

c++ - 扫描字符串字符的 ASCII 值

转载 作者:行者123 更新时间:2023-11-30 01:50:44 26 4
gpt4 key购买 nike

我想检查一个字符串是否包含 0-9 或 A-Z 以外的任何字符,如果包含,则停止程序。这就是我所做的:

string number_in;

for (int i = 0; number_in[i] == '\0'; i++)
{
if ( (number_in[i] < 48) || ( (number_in[i] > 57) && (number_in[i] < 65) ) || (number_in[i] > 90) )
{
cout << "\nInput number contains incorrect characters!\n";
getchar;
return 0;
}
}

但是无论我输入哪个字符串,它总是会跳过 for 循环。为什么会这样?

最佳答案

number_in[i] == '\0' 似乎不正确。这是循环继续运行的条件。

但是,使用 std::isalnum 有一个更简单的解决方案和 std::all_of :

bool stopProgramm = !std::all_of( std::begin(str), std::end(str),
[] (unsigned char c)
{ return std::isdigit(c) || std::isupper(c); } );

关于c++ - 扫描字符串字符的 ASCII 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27070059/

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