1

I'm getting the following runtime error with boost::multiprecision::cpp_int:

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

The function randomCppInt generates a random cpp_int of a specified length. The program just loops through displaying these but terminates with the error randomly - sometimes on the first pass through the loop, othertimes after a few iterations. I can't seem to identify where I'm going wrong.

#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;
}
Jed
  • 11
  • 2

0 Answers0