繁体   English   中英

C ++ find_first_not_of

[英]C++ find_first_not_of

我是一位c ++初学者,对此我感到困惑,我们将不胜感激。

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string str;
    cout << "CONVERSION\n\n";
    cout << "Base 11 to Decimal\n";
    cout << "Base 11: ";
    getline(std::cin, str);
    const auto bad_loc = str.find_first_not_of("0123456789aA");
    if (bad_loc != std::string::npos) 
    {
        throw "bad input"; // or whatever handling
    }
    unsigned long ul = std::stoul(str, nullptr, 11);
    cout << "Decimal: " << ul << '\n';
    return 0;
}

输出是

CONVERSION

Base 11 to Decimal
Base 11: 1234AB 

该程序停止了,并且没有向我发送“错误的输入”。 找不到任何解决方案。 提前致谢

BoBTFish在评论中给出了答案:

好吧,您不会捕获并处理抛出的字符串。 因此,您的程序将刚刚退出,并且您的操作系统将执行其所有操作,而这可能不包括尝试打印字符串。 就本测试而言,更换起来可能更简单

 throw "bad input"; 

 std::cerr << "bad input\\n"; return 1; 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM