【发布时间】:2012-03-27 09:21:37
【问题描述】:
我正在使用 c++ 开发一个项目(我刚开始学习),但不明白为什么这个功能不起作用。我正在尝试使用变量 first_name 编写“Person”类,并使用函数 set_first_name 来设置名称。 Set_first_name 需要调用一个函数(下面的那个)来检查名称中是否有任何数字。该函数总是返回false,我想知道为什么?另外,这是检查数字的最佳方法,还是有更好的方法?
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;
}
【问题讨论】:
标签: c++ class function numbers