gpt4 book ai didi

C++ find_first_not_of

转载 作者:行者123 更新时间:2023-11-30 05:30:54 29 4
gpt4 key购买 nike

我是一名 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在评论中给出了答案:

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;

关于C++ find_first_not_of,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35741343/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com