Jake Wickham Jake Wickham - 1 year ago 104
C++ Question

How would I check if a string contains only numeric digits?

I require a way to check if a string contains only the numbers "123456789". I have been writing a cruise management system and this is crucial for the part where you register a phone number. I've searched through the string library reference but was unable to come up with anything. Thank you for your time.

I was searching here

Answer Source

Use std::all_of:

#include <algorithm>
#include <string>
#include <cctype>

bool allDigits(const std::string& s)
   return std::all_of(s.begin(), s.end(), ::isdigit);

Live Example

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download