简体   繁体   English

C ++ find_first_not_of

[英]C++ find_first_not_of

Im a c++ beginner and I'm getting confused with this one, any help would be much appreciated. 我是一位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;
}

The output was 输出是

CONVERSION

Base 11 to Decimal
Base 11: 1234AB 

The program stopped and it didn't send me the "bad input". 该程序停止了,并且没有向我发送“错误的输入”。 Couldn't find any solution. 找不到任何解决方案。 Thanks in advance 提前致谢

BoBTFish gave the answer in a comment: BoBTFish在评论中给出了答案:

Well you don't catch and handle the thrown string. 好吧,您不会捕获并处理抛出的字符串。 So your program will just exit, and your OS will do whatever it does, which may not include attempting to print the string. 因此,您的程序将刚刚退出,并且您的操作系统将执行其所有操作,而这可能不包括尝试打印字符串。 For the purposes of this test, it's probably simpler to replace 就本测试而言,更换起来可能更简单

 throw "bad input"; 

with

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

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

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