I am new to C++ and am probably missing something obvious. I want to converse OCT and HEX numbers to DEC. I can get all kinds of wrong inputs, so instead of many conditions I wanted to make the conversion with std::stoul and catch when it fails.
I get the numbers from a vector vec of strings, but that is working fine and I don't think the problem is there.
Problem is that whenever the method fails (for example "88" for OCT) the whole test fails and it is not going into the catch block.
This is my code:
//1 - Octan number
unsigned long octDec;
try {
octDec = std::stoul(vec.at(1), nullptr, 8);
}
catch(const std::invalid_argument& ia){
return false;
}
//2 - Hex number
std::string h = vec.at(3).c_str();
unsigned long hexDec;
try{
hexDec = std::stoul(h, nullptr, 16);
}
catch(const std::invalid_argument& ia) {
return false;
}
Aucun commentaire:
Enregistrer un commentaire