文字列全体を反復処理して整数を出力することにより、文字列に整数が含まれているかどうかをテストしようとしています。私の方法では、文字列を c-string 、c-string に変換し、関数atoi
を使用して整数かどうかをテストし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());
}
}
}