Function to check if a string contains a number

I am working on a C ++ project (which I was just starting to learn) and cannot understand why this function does not work. I am trying to write a "Person" class with the variable first_name and use the set_first_name function to set the name. Set_first_name, you must call the function (listed below) to check if the name has any numbers in it. The function always returns false, and I wonder why? Also, is this the best way to check the number, or is there a better way?

bool Person::contains_number(std::string c){ // checks if a string contains a number if (c.find('0') == std::string::npos || c.find('1') == std::string::npos || c.find('2') == std::string::npos || c.find('3') == std::string::npos || c.find('4') == std::string::npos || c.find('5') == std::string::npos || c.find('6') == std::string::npos || c.find('7') == std::string::npos || c.find('8') == std::string::npos || c.find('9') == std::string::npos){// checks if it contains number return false; } return true; } 
+3
c ++ function class numbers
source share
5 answers

It always returns false because your logic is reversed. You are using the || with checks == npos . If there is no one digit in the line, == npos is evaluated to true and || so you return false . You need to use the checks != npos , and then return true if any check evaluates to true :

 bool Person::contains_number(const std::string &c) { if (c.find('0') != std::string::npos || c.find('1') != std::string::npos || c.find('2') != std::string::npos || c.find('3') != std::string::npos || c.find('4') != std::string::npos || c.find('5') != std::string::npos || c.find('6') != std::string::npos || c.find('7') != std::string::npos || c.find('8') != std::string::npos || c.find('9') != std::string::npos) { return true; } return false; } 

Or:

 bool Person::contains_number(const std::string &c) { return ( c.find('0') != std::string::npos || c.find('1') != std::string::npos || c.find('2') != std::string::npos || c.find('3') != std::string::npos || c.find('4') != std::string::npos || c.find('5') != std::string::npos || c.find('6') != std::string::npos || c.find('7') != std::string::npos || c.find('8') != std::string::npos || c.find('9') != std::string::npos ); } 

A simpler solution is to use find_first_of() instead of find() :

 bool Person::contains_number(const std::string &c) { return (c.find_first_of("0123456789") != std::string::npos); } 
+5
source share

Change all your || at && .

Even better:

 return std::find_if(s.begin(), s.end(), ::isdigit) != s.end(); 

Or if you have this:

 return std::any_of(s.begin(), s.end(), ::isdigit); 
+10
source share

C ++ 11:

 #include <algorithm> #include <cctype> #include <string> #include <iostream> bool has_any_digits(const std::string& s) { return std::any_of(s.begin(), s.end(), ::isdigit); } int main() { std::string query("H311o, W0r1d!"); std::cout << query << ": has digits: " << std::boolalpha << has_any_digits(query) << std::endl; return 1; } 

Output:

H311o, W0r1d!: has digits: true

+7
source share

How to check if a string contains any digits in C ++

That should do it!

 if (std::string::npos != s.find_first_of("0123456789")) { std::cout << "digit(s)found!" << std::endl; } 
+4
source share

You are using || (or operator) to check for several conditions in an if statement. The or operator returns true (satisfies the condition) if one of the expressions is true.

The operator or first evaluates the expression to its left: if it is true, then it does not evaluate the expression to the right and returns true. If the expression on the left is false, then the expression on the right is evaluated and its result is returned as the result || Operator

This is what happens in your function:

  • contains c '0'? if not (since std :: string :: npos in find () means it was not found), return false
  • contains c '1'? if no then return false
  • ...

So, replace the or operators with && (and the operator).

+1
source share

All Articles