# Thread: Check if variable is integer

1. ## 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++.

2. 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.

3. 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. If the above matches the description, wouldn't the result only be integral if number is 0 or 2?

5. Originally Posted by Elkvis
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. 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.