【问题标题】:std::cin>> is digit or stringstd::cin>> 是数字或字符串
【发布时间】:2022-09-27 15:18:08
【问题描述】:

我必须确定输入是数字还是字符串。

std::string s;
while (std::cin >> s) { 
    if(isdigit(s)){
        //do something with the variable
    }
    else{
        //do something else with the variable
    }
}

为此我得到 error: no matching function for call to \'isdigit(std::__cxx11::string&)\' 有人可以提出我应该使用的方法吗?

  • 有std::isdigit(),但它不带字符串;它需要性格。

标签: c++ cin stdstring typechecking


【解决方案1】:
std::string s;
while (std::cin >> s) { 
    if(s.size() == 1 && isdigit(s[0])){
        //do something with the variable
    }
    else{
        //do something else with the variable
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-17
    • 2020-10-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多