转换基数为11到十进制

时间:2016-03-09 16:34:41

标签: c++ base

我知道unsigned long仅限于4294967295,所以这是我的问题,当用户输入多个数字时(比如结果将超出限制),转换的数字将是只是4294967295。 例如:

Base 11: 1928374192847
Decimal: 4294967295

结果应该是5777758712535.如何修复此限制? vb6.0需要我们。 这是我的代码:

    cout << "\t\t  CONVERSION\n";
    cout << "\t     Base 11 to Decimal\n";
    cout << "\nBase 11: ";
    cin >> str;
    const auto x = str.find_first_not_of("0123456789aA");
    if (x != string::npos)
    {
        std::cout << "Invalid Input\n\n";
        goto a;
    }
    unsigned long x = strtoul(str.c_str(), NULL, 11);
    cout << "Decimal: " << x << "\n\n";

该计划应该说&#34;超出范围&#34;如果结果将超过4294967295。 对不起,我只是一个初学者。

1 个答案:

答案 0 :(得分:2)

来自strtoul docs on www.cplusplus.com

  

如果读取的值超出unsigned long int的可表示值范围,则函数返回ULONG_MAX(在&lt; climits&gt;中定义),并且errno设置为ERANGE。

您应该检查错误以确定该值是否超出范围。

相关问题