Im a c++ beginner and I'm getting confused with this one, any help would be much appreciated.
#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:
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;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.