[Solved] Catch an exception when user enters integer value in character array [closed]


Use std::none_of, along with isdigit:

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

int main()
{
   std::string test = "abc123";
   if ( std::none_of(test.begin(), test.end(), ::isdigit))
      std::cout << "All good\n"; 
   else
      std::cout << "You've entered an integer\n";

   // Try with good data
   test = "abcdef";
   if ( std::none_of(test.begin(), test.end(), ::isdigit))
      std::cout << "All good\n"; 
   else
      std::cout << "You've entered an integer\n";    
} 

Live Example

solved Catch an exception when user enters integer value in character array [closed]