不能将10个以上的数字放入c ++中的一个变量中

时间:2018-01-31 04:00:32

标签: c++ c++11 variables

我想创建一个循环,它接受一个字符串输入数字并将其转换为整数变量,这样我就可以将数字加1,然后将其转回一个字符串并将其打印出5次,其中1加入到每次打印的数量。问题是,它最多只能工作9位数。如果你放了9个以上,那么就开始给出其他不同的数字,比如-125346543。

如果您输入9位数字101010101,则会将其打印出来101010101 - > 101010106,但如果您输入10位数字,如1010101010.它将打印出来-128764798 - > 128764804.我需要它打印最少13位数

#include <iostream>
#include <string>
#include <sstream>

int main() {
    int l=0;
    int x=0;
    string nope;
    cout<<"please provide 13 digit code : ";
    cin>>nope;                                // ask for 13 digit code
for(i=0; i < 5 ; i++)
{
    stringstream geek(nope);                 // turn string to Int
    geek>>x;
    l = x + 1;                               // add 1 to Int
    stringstream ss;                         // turn it back to string
    ss << l;
    string q = ss.str();
    nope = q;
    cout<<nope;                              // prints out
}

1 个答案:

答案 0 :(得分:1)

C ++中的内置int类型通常是32位。这意味着它们不能存储大于2 ^ 31-1的值(因为高位通常用于符号)。 (C ++要求它至少为16位)

如果您需要无限精度整数,则必须使用不直接在int中构建的库。

相关问题