Jake Wickham Jake Wickham - 2 months ago 7x
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


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