Using STL's count_if function in combination with isdigit to count how many non-digit characters there are (also some help from the not1 and ptr_fun function adapters):
Code:
#include <string>
#include <algorithm>
#include <functional>
#include <cctype>
using namespace std;
...
string str1 = "1-23";
string str2 = "12 ";
string str3 = "123";
cout << "\"" << str1;
if( count_if( str1.begin(), str1.end(), not1(ptr_fun(isdigit)) ) )
cout << "\" is not a number." << endl;
else cout << "\" is a number." << endl;
cout << "\"" << str2;
if( count_if( str2.begin(), str2.end(), not1(ptr_fun(isdigit)) ) )
cout << "\" is not a number." << endl;
else cout << "\" is a number." << endl;
cout << "\"" << str3;
if( count_if( str3.begin(), str3.end(), not1(ptr_fun(isdigit)) ) )
cout << "\" is not a number." << endl;
else cout << "\" is a number." << endl;
Should output:
Code:
"1-23" is not a number.
"12 " is not a number.
"123" is a number.