It's clumsy enough to use that an ad hoc solution may be easier to use, but the standard library actually supports this directly:
#include <locale>
#include <iostream>
#include <iomanip>
int main() {
char *inputs[] = {
"0983",
"124test"
};
std::locale loc(std::locale::classic());
std::ctype_base::mask m = std::ctype_base::digit;
for (int i=0; i<2; i++) {
char const *b = inputs[i];
char const *e = b + strlen(b);
std::cout << "Input: " << std::setw(10) << inputs[i] << ":\t";
if (std::use_facet<std::ctype<char> >(loc).scan_not(m, b, e) == e)
std::cout << "All Digits\n";
else
std::cout << "Non digit\n";
}
return 0;
}
If you're using C++11, std::all_of
is almost certainly easier to use:
#include <string>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <ctype.h>
int main() {
std::string inputs[] = {
"0983",
"124test"
};
std::cout << std::boolalpha;
for (int i=0; i<2; i++)
std::cout << std::setw(10) << inputs[i] << "\tAll digits?: "
<< std::all_of(inputs[i].begin(), inputs[i].end(), ::isdigit) << "\n";
return 0;
}