文字列が数値ならtrueを返す関数を作りたいのですが、うまくいきません。
"-2"など負の数を入力するとfalseを返します。
```C++
bool checkInt(std::string str)
{
if (std::all_of(str.cbegin(), str.cend(), isdigit))
{
return true;
}
return false;
}
(文字列が数値の場合、次の関数で文字列を数値に変換することを試みています。) ```C++ int inputNum() { string str; int num; cin >> str; if (checkInt(str)) { num = stoi(str); return num; } else { cout << "Error!" << endl; cin >> str; } }
C++初心者にアドバイスのほど、よろしくお願いしますm(_ _)m

回答4件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。