I am trying to put a large number inside an int variable but I am having problems. The compiler I am using is microsofts visual C++ compiler v6.0.
I get this error.
error C2177: constant too big
I thought that directly declaring it as an int wouldn't work because only 4 bytes is allocated to an int so I tried using new to allocate enough space.
Code:
int* number = new int [8];
number = 39990881947540732160326;
It still says constant too big.
I tried declaring the number as type char* then using atoi and atol to convert it into a large integer but that didn't work using the code below.
Code:
char* number = "39990881947540732160326";
int number2 = atol(number);
cout<<number2;
Then I tried declaring it as a type char* again, creating an integer with new large enough to hold the correct number of bytes and then using a for loop, to move each number for the char pointer into the large int array. Like below.
Code:
char* number = "39990881947540732160326";
int* number2 = new int [strlen(number)];
for(int i = 0; i < strlen(number); i++){
number2[i] = number[i];
}
cout<<number2;
This still doesn't work
I realise I over allocate space in the example above, it should be
shouldn't it?
But if I did that wouldn't it mean there are not enough elements of the array for me to store each number in? Like if I do it the first way isn't there enough elements but each element can hold 4 numbers right? Or should I do a nested loop inside the other loop, moving 4 numbers into one element of the array somehow, so that I can create a proper sized integer pointer so that there is not extra space allocated.
Anyway, I can worry about proper space allocation later , if anybody can help it is appreciated.