如何验证字符串中只有数字? 我是C++新手。我正在做一个项目,我需要通过控制台从用户那里读取大部分整数。为了避免有人输入非数字字符,我考虑将输入读取为字符串,检查其中只有数字,然后将其转换为整数。我创建了一个函数,因为我需要多次检查整数: bool isanInt(int *y){ string z; int x; getline(cin,z); for (int n=0; n < z.length(); n++) { if(!((z[n] >= '0' && z[n] <= '9') || z[n] == ' ') ){ cout << "That is not a valid input!" << endl; return false; } } istringstream convert(z); //converting the string to integer convert >> x; *y = x; return true; } bool-isanInt(int*y){ 字符串z; int x; getline(cin,z); 对于(int n=0;n='0'&&z[n]

如何验证字符串中只有数字? 我是C++新手。我正在做一个项目,我需要通过控制台从用户那里读取大部分整数。为了避免有人输入非数字字符,我考虑将输入读取为字符串,检查其中只有数字,然后将其转换为整数。我创建了一个函数,因为我需要多次检查整数: bool isanInt(int *y){ string z; int x; getline(cin,z); for (int n=0; n < z.length(); n++) { if(!((z[n] >= '0' && z[n] <= '9') || z[n] == ' ') ){ cout << "That is not a valid input!" << endl; return false; } } istringstream convert(z); //converting the string to integer convert >> x; *y = x; return true; } bool-isanInt(int*y){ 字符串z; int x; getline(cin,z); 对于(int n=0;n='0'&&z[n],c++,C++,您可以在try/catch块中强制转换字符串,这样,如果强制转换失败,它将引发异常,您可以在控制台中写入任何您想要的内容 例如: try { int myNum = strtoint(myString); } catch (std::bad_cast& bc) { std::cerr << "Please insert only numbers "<< '\n'; } 试试看 { int myNum=strotint(m

您可以在try/catch块中强制转换字符串,这样,如果强制转换失败,它将引发异常,您可以在控制台中写入任何您想要的内容

例如:

  try
  {
    int myNum = strtoint(myString);
  }
  catch (std::bad_cast& bc)
  {
     std::cerr << "Please insert only numbers "<< '\n';
  }
试试看
{
int myNum=strotint(myString);
}
捕获(标准:坏的\u铸造和bc)
{

有很多方法只测试字符串中的数字字符

bool is_digits(const std::string &str) {
    return str.find_first_not_of("0123456789") == std::string::npos;
}

始终使用现成的函数。切勿单独编写。 我推荐


享受。

尝试另一种方法,如cin.getline(str,sizeof(str)),str这里是char*。我认为在调用此函数之前,您的问题可能是由其他函数引起的。也许您可以仔细检查代码的其他部分。建议也设置断点。

这将起作用:

#include <algorithm> // for std::all_of
#include <cctype>    // for std::isdigit

bool all_digits(const std::string& s)
{
  return std::all_of(s.begin(), 
                     s.end(), 
                     [](char c) { return std::isdigit(c); });
}
\include//for std::all\u of
#包含//用于std::isdigit
布尔所有数字(常量标准::字符串和s)
{
return std::所有的(s.begin(),
s、 end(),
[](字符c){return std::isdigit(c);});
}

字符分类是一项通常委托给区域设置的
ctype
方面的工作。您需要一个考虑所有9位数字(包括千位分隔符和小数点)的函数:

bool is_numeric_string(const std::string& str, std::locale loc = std::locale())
{
    using ctype = std::ctype<char>;
    using numpunct = std::numpunct<char>;
    using traits_type =  std::string::traits_type;

    auto& ct_f  = std::use_facet<ctype>(loc);
    auto& np_f = std::use_facet<numpunct>(loc);

    return std::all_of(str.begin(), str.end(), [&str, &ct_f, &np_f] (char c)
    {
        return ct_f.is(std::ctype_base::digit, c) || traits_type::eq(c, np_f.thousands_sep())
                                                 || traits_type::eq(c, np_f.decimal_point());
    });
}
bool是数字字符串(const std::string&str,std::locale loc=std::locale()) { 使用ctype=std::ctype; 使用numpunct=std::numpunct; 使用traits\u type=std::string::traits\u type; auto&ct\u f=std::use\u facet(loc); auto&np\u f=std::use\u facet(loc); 返回std::all_of(str.begin()、str.end()、[&str、&ct_f、&np_f](字符c) { 返回ct_f.is(std::ctype_base::digit,c)| | traits_type::eq(c,np_f.数千_sep()) ||特征类型::等式(c,np_f.小数点()); }); }

注意,可以确保数千个分隔符不是第一个字符。

只需执行<代码> CIN > z <代码>,而不是<代码> GETLINE 。<代码> x>代码>然后是代码> Z.Langthh()/Case>。如果您真的想读取整数,可以通过C++简单地编码来检查C++:<代码> int Myint;(std::cin>>my_int)…得到了一个int.
;否则
…不是int或EOF…这并不能回答问题。将字符串转换为int的自定义函数