Thread: Check if variable is integer

  1. #1
    Registered User
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    2

    Check if variable is integer

    Good afternoon,

    Ive searched on alot of website for this but allways came with diffrent results but never the answer for my problem. Im sure there must be an really easy way to solve this. Im just new in c++ so bare with me, actually started last week.

    My problem is:

    I want the user to put in a number. Like this:

    cout << " Give a number: ";
    cin >> number;

    Then I got an other variable called number2. Number2 is allways number minus 1. So

    number2 = number - 1;

    Now I devide number by number 2 and get an result. For example if number is 5, number2 is 4 so result is 1.25. The result I stored in an other float variable (a). Now I wanne check if a is a integer number or not.

    In php theres an really easy solution for this: is_integer($a); , Unfortuantly I havent found such function in c++.

    I hope ive made my problem clear. Thanks in advance

    - Adoro

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You probably have a background in a programming language where variables are dynamically typed, so $a can be an integer, and then suddenly become a floating point variable after an assignment.

    In C++, an int variable cannot become a float. Consider:
    Code:
    int x = 7;
    x /= 2;
    Now, x is 3, not 3.5, since integer division results in truncation.

    Consequently, to determine if the mathematical result of number / number2 will be an integer, you should ask a different question: is number perfectly divisible by number2? If so, then number / number2 will be an integer. So, compute number &#37; number2. If the result is 0, then number is perfectly divisible by number2.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    I don't think the modulus (&#37 operator would be effective on non-integer input types. It could be a problem if the user wanted to use floating point values of some sort.

    My understanding of this question is that the OP wants to know if the result is mathematically an integer - a positive or negative (or zero) number that has no fractional portion - not an integer in the C/C++ context.

    Try the following. The number in question can be a floating-point or integer:
    Code:
    double number, number2, result;
    long num;
    cout << "Enter a number: ";
    cin >> number;
    number2 = number - 1;
    result = number / number2;
    num = (long) result;
    if (result == num) cout << "result is an integer";
    This may not be a completely effective solution, given the fact that floating point numbers can only approximate integer values, and are never exact. As I understand floating point data types, the only numbers that can be stored exactly are -1, 0, 1, and any power of 10 within the range of the type. I could be wrong about this, so don't take it as gospel truth or anything.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    If the above matches the description, wouldn't the result only be integral if number is 0 or 2?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    2
    Quote Originally Posted by Elkvis View Post
    I don't think the modulus (&#37 operator would be effective on non-integer input types. It could be a problem if the user wanted to use floating point values of some sort.

    My understanding of this question is that the OP wants to know if the result is mathematically an integer - a positive or negative (or zero) number that has no fractional portion - not an integer in the C/C++ context.

    Try the following. The number in question can be a floating-point or integer:
    Code:
    double number, number2, result;
    long num;
    cout << "Enter a number: ";
    cin >> number;
    number2 = number - 1;
    result = number / number2;
    num = (long) result;
    if (result == num) cout << "result is an integer";
    This may not be a completely effective solution, given the fact that floating point numbers can only approximate integer values, and are never exact. As I understand floating point data types, the only numbers that can be stored exactly are -1, 0, 1, and any power of 10 within the range of the type. I could be wrong about this, so don't take it as gospel truth or anything.
    Yes this is what I mean, thanks. Will work futher with this

    Thanks

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    A number can be exactly represented in a float (assuming IEEE) if the number of digits needed to write it in binary fit in the mantissa part of the float (23 bits plus the implicit one). (Note that the only non-repeating fractions in binary are powers-of-two fractions, 1/2, 1/4, 1/8, etc.)

    There is such a thing as modf too.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HELP!!!!emergency Problem~expert please help
    By unknowppl in forum C++ Programming
    Replies: 9
    Last Post: 08-21-2008, 06:41 PM
  2. HELP!!!!emergency ~expert please help
    By unknowppl in forum C Programming
    Replies: 1
    Last Post: 08-19-2008, 07:35 AM
  3. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  4. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM