1

我在 boost::multiprecision::cpp_int 中遇到以下运行时错误:

terminate called after throwing an instance of 'boost::wrapexcept<std::runtime_error>'
what():  Unexpected content found while parsing character string.

函数 randomCppInt 生成一个指定长度的随机 cpp_int。程序只是循环显示这些,但随机终止并出现错误 - 有时在第一次通过循环时,有时在几次迭代后。我似乎无法确定我哪里出错了。

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <boost/multiprecision/cpp_int.hpp>

using namespace boost::multiprecision; 
using namespace std;

// Generates a random cpp_int of specified length.
cpp_int randomCppInt(cpp_int length)
{
    // Seed RNG with time.
    srand(time(0));
    string num = "";

    // Iterate for length
    for (cpp_int i = 0; i < length; i++)
    {
        // Random char from '0' to '9'
        char ch = (rand() % 10) + 48;
        // Add to string.
        num += ch;
    }

    // Init new cpp_int using string and return to caller.
    cpp_int result(num);
    return result;
}


int main()
{

    // For testing, just generate randoms and cout for now.
    while (true)
    {
        cpp_int num = randomCppInt(5432);
        cout << num << endl;    
    }

    return 0;
}
4

0 回答 0