Thread: Constant too big error

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    63

    Constant too big error

    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
    Code:
     strlen(number) / 4
    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.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    #include <iostream>
    #include <climits>
    
    int main()
    {
       std::cout << "INT_MAX = " << INT_MAX << std::endl;
       return 0;
    }
    
    /* my output
    INT_MAX = 2147483647
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    63
    I see.

    So as that is the maximum size for an int on my processor aswell, I will need to use another data type right?

  4. #4
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    You can try a long int:
    Code:
    long number2;
    But I dont think a long can get that big either.
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Yes, you'll need another data type.

    If you're just after an answer, then look up GMP
    http://www.swox.com/gmp/

  6. #6
    Registered User
    Join Date
    Jan 2006
    Posts
    63
    Quote Originally Posted by Blackroot
    You can try a long int:
    Code:
    long number2;
    But I dont think a long can get that big either.
    LONG_MAX gives the same as INT_MAX.


    Quote Originally Posted by Salem
    Yes, you'll need another data type.

    If you're just after an answer, then look up GMP
    http://www.swox.com/gmp/

    I will look into it thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  2. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  3. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  4. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 04:49 AM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM