[英]Conversion from base 11 to 10 c++
This code can convert from base 11 to base 10: 此代码可以从11转换为10:
#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);
unsigned long ul = std::stoul (str,nullptr,11);
cout << "Decimal: " << ul << '\n';
return 0;
}
But when i enter BZ that is not included to base 11, the program stops, what i want to happen is like this 但是当我输入不包含在base 11中的BZ时,程序停止运行,我想发生的事情是这样的
if the user enters invalid variables, the program should say "Invalid Input". 如果用户输入无效变量,则程序应显示“ Invalid Input”。 please help
请帮忙
You can use std::string::find_first_not_of
您可以使用
std::string::find_first_not_of
...
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);
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.