Thread: Newbies question about the integer type

  1. #1
    Unregistered
    Guest

    Angry Newbies question about the integer type

    Hello engineers

    I have a fundamental question and I think it somewhere on the c++ book but I could not find it. Any help would be very appreciates.

    Question: I am doing OK for the n! for integer type from 1 to 16. But I have problem when i start to enter 17. What is the problem here. Is this because the integer type?

    Here is the program

    #include <iostream.h>

    int main()

    {
    int in, out;
    out = 1;
    cout << "Enter input number \n\n";
    cin >> in;

    for (int x = 1; x <= in ; x ++)
    out = x * out;
    cout << "Here are the result" <<out<<endl;

    return 0;
    }

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    Depending on your system and compiler, an int is a number
    that can be stored in 2 or 4 bytes ( 16 or 32bit system ).

    2 bytes can hold 65536 different values. By default an int is
    signed, so it can hold the values -32768 to +32768. If you add
    one to the max value, it will revert to the minimum value.
    Likewise, subtracting one from the minimum will result in the value
    reverting to the maximum.
    You could use an unsigned int, as this would hold values from
    0 to 65535 but that won't solve your problem.

    With four bytes, it's likewise, just larger. A four-byte int
    ( as I assume your system uses ) will hold 2^32 =
    4.294.967.296 different values... ( -2147483648 to
    +2147483648 )... so if your number is larger than that,
    you'll need another datatype.

    I don't know of any standard datatype around though.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    47
    using an unsigned _int64 would give you the best results if you're only using positive numbers. If you need something even bigger, then you may have to code a class that strings together a whole bunch of unsigned longs and manipulates them as a single composite value.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  2. strings Vs. Char pointers
    By aijazbaig1 in forum C Programming
    Replies: 49
    Last Post: 02-13-2008, 09:51 AM
  3. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM
  4. header file bringing errors?
    By bluehead in forum Windows Programming
    Replies: 4
    Last Post: 08-19-2003, 12:51 PM
  5. qwerty/azerty keyboard type problem + question about loop.
    By Robin Hood in forum C++ Programming
    Replies: 9
    Last Post: 07-22-2002, 01:03 PM