2147483647 maximum integer

How do I use a vector to store a number that's bigger than 2147483647? I'm reading in an insanely large number and manipulating the digits. When the user input is anything above 2147483647 (i.e. 2147483648), it will just stop at 2147483647.
If you're just working with individual digits, i would read the number into a std::string.
My apologies, I actually used stringstream to convert the vector of ints into a string, and then read each individual digits.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
    stringstream os;
    string st;
    
    //loop through the number of strings (in this case, 4 total strings)
    for (int j = 0; j < in; j++) {
        os << v.at(j); //converting int 1112 to a string so we can iterate through it
        st = os.str(); //store the newly converted string into a string variable
        
        //loop through each individual digit of each string (in this case, 4 digits per string)
        //starting from 1 to length-1 to avoid the edges, which we don't care about
        for (int k = 1; k < st.length()-1; k++) {
            //if current is greater than both neighbors, change that to 'X'
            if (st.at(k) > st.at(k+1) && st.at(k) > st.at(k-1)) {
                st.at(k) = 'X'; //replace that digit with an 'X'
            }
        }
        
        cout << st << endl;
        os.str(string());  //clears the string in the vector so we can take the next input
        os.clear();


st is printing out 2147483647 no matter what.
The largest value an integer can hold in C++ is 231-1, which just so happens to be 2147483647.

Use double or float.

http://www.cplusplus.com/doc/tutorial/variables/
If your compiler supports C++11, you can use the long long type, which is 64-bits.
Use long long or unsigned long long if the numbers are within their range.
http://en.cppreference.com/w/cpp/language/types

If not, use a large integer library; for instance boost::multiprecision::cpp_int
http://www.boost.org/doc/libs/1_57_0/libs/multiprecision/doc/html/index.html

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
#include <iostream>
#include <string>
#include <limits>
#include <boost/multiprecision/cpp_int.hpp>

int main()
{
    std::cout << std::numeric_limits<long long>::max() << '\n' ; // typically 9223372036854775807
    std::cout << std::numeric_limits<unsigned long long>::max() << '\n' ; // typically 18446744073709551615

    {
        int i = 15 ;
        unsigned long long n = 1 ;
        while( i > 1 ) { n *= i ; --i ; }
        std::cout << "15! == " << n << '\n' ;

        const std::string str = std::to_string(n) ;
        std::cout << str.size() << " digits\n" ;
    }

    {
        int i = 50 ;
        boost::multiprecision::cpp_int n = 1 ;
        while( i > 1 ) { n *= i ; --i ; }
        std::cout << "50! == " << n << '\n' ;

        const std::string str = n.str() ;
        std::cout << str.size() << " digits\n" ;
    }
}

http://coliru.stacked-crooked.com/a/f432c9fb25a17d88
@pattrick128 Instead of reading the numbers into a vector<int> and then converting to string why not cut out the middle man and read into a vector<string>?
Topic archived. No new replies allowed.