我正在尝试通过遍历整个字符串并输出整数来测试字符串是否包含整数。我的方法涉及将字符串转换为c-string,使用
我需要帮助解决这个问题。
atoi
函数将c-string转换为整数,然后使用isdigit
函数测试它是否为整数。出于某种未知的原因,isdigit
函数返回false,即使它遇到了一个整数。我需要帮助解决这个问题。
#include <iostream>
using namespace std;
#include <string>
int main()
{
string p = "[ab, e2cd]";
for (int k = 0; k < p.length(); k++)
{
if (isdigit(atoi(p.substr(k,1).c_str())) == true) //testing done here
{
cout << atoi(p.substr(k, 1).c_str());
}
}
}
if (isdigit(*(p.substr(k,1).c_str())) == true) //testing done here
进行快速而不太规范的更改。 - abasu