Big Integer Problem in C++

Input a number from user having more than 10 digits and less than 45

Store 9 digit number at every index of int arr[5]

I have problem on how to take input from user

Example if user Enter =1234567898765543211234

int arr[5]={123456789,876554321,1234,0,0};

Example if user Enter 123456789987654321123456789454565456123457890

int array[5]={123456789,987654321,123456789,454565456,123457890}
Read the input into a string, check that the size is right and then use substr to extract the 9 digits (add zeros if necessary) and use stoi to convert to int, finnaly store it in your array
Read the input in as a string.

Extract substrings of the required size. https://en.cppreference.com/w/cpp/string/basic_string/substr

Convert each substring to int and store in the array. https://en.cppreference.com/w/cpp/string/basic_string/stol

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <string>
#include <cctype>
#include <vector>
#include <limits>
#include <algorithm>

std::string get_int_as_string()
{
    std::string result ;

    std::cout << "enter the digits of an integer. end input with a new line\n"
              << "note: non-decimal-digit characters will be ignored)\n" ;
    char c ;
    while( std::cin.get(c) && c != '\n' )
        if( std::isdigit(c) ) result += c ;

    return result ;
}

std::vector<int> split( const std::string& int_str )
{
    constexpr std::size_t ndigits = std::numeric_limits<int>::digits10 ;

    std::vector<int> result ;

    if( std::all_of( int_str.begin(), int_str.end(), [] ( char c ) { return std::isdigit(c) ; } ) )
    {
        for( std::size_t pos = 0 ; pos < int_str.size() ; pos += ndigits )
            result.push_back( std::stoi( int_str.substr( pos, ndigits ) ) ) ;
    }

    return result ;
}

template < std::size_t ARRAY_SIZE >
void split_into( const std::string& int_str, int(&array)[ARRAY_SIZE] )
{
    std::vector<int> numbers = split(int_str) ;
    numbers.resize(ARRAY_SIZE) ;
    std::copy( numbers.begin(), numbers.end(), std::begin(array) ) ;
}

int main()
{
    const std::string int_string = get_int_as_string() ;
    std::cout << int_string << '\n' ;

    int array[5] ;
    split_into( int_string, array ) ;
    for( int v : array ) std::cout << v << '\n';
}

http://coliru.stacked-crooked.com/a/12bb4f7e194102c4
# include <iostream>
# include <string>
using namespace std;
int main()
{

string s ("1300000000000900000000100000000");
string s1[5]="";

s1[0]=s.substr(0,9);
s1[1]=s.substr(10,18);


cout << s1[0]<<endl;
cout << s1[1]<<endl;

return 0;
}
It is not giving correct value for s1[1];
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>

int main()
{

    std::string s ("1300000000000900000000100000000");
    std::cout << s << '\n' ;

    std::string s1[5] ;
    s1[0] = s.substr(0,9); // first nine digits
    s1[1] = s.substr(9,9); // next nine digits 
    s1[2] = s.substr(18,9); // third set of nine digits

    std::cout << s1[0] << '\n' << s1[1] << '\n' << s1[2] << '\n' ;
}
The boost libraries offer this functionality Chapter 1. Boost.Multiprecision

For example:

#include <boost/multiprecision/cpp_int.hpp>
#include <iostream>
namespace mp = boost::multiprecision;

int main()
{
mp::cpp_int u = 1;
for(unsigned i = 1; i <= 100; ++i)
u *= i;

std::cout << "100! = " << u << '\n';
}

If you want to know more about this a step by step guide with examples on how to use the library. Visit here:- http://www.traininginlucknow.in/best-c-language-training-in-Lucknow.html
Topic archived. No new replies allowed.