# Thread: checking for non-integer values

1. ## checking for non-integer values

Hi! I am working on a program to add, subtract, multiply and divide fractions and I need some way to check that the numerators and denominators that the user enters are not non-integer values. Does anyone know the best way to do this? I checked out the isdigit() function, but the data type of the user input for the denominators and numerators are type int, not a string. Thanks in advance!

Smiles,
ibleedart

2. Something akin to this?

3. Assuming you already know that the input is a number, and you just want to make sure it isn't fractional, you can test if the truncation of the value is equal to the value itself:

Code:
```double val; /* Comes from input */

if( (int)val != val )
{
/* The number is not an integer */
}```

4. This is the way I like to recommend. It evaluates the return value of the input, which will be false if the user enters something that is not a number. It can be expanded to fail if the user enters a number followed by a letter if you want to do that (e.g. you want to fail on 123abc), just ask or search.
Code:
```int number = 0;

// ask user for number here
while (!(std::cin >> number))
{
std::cin.clear(); // clear fail state
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // ignore bad input
// warn user and re-prompt here
}```
Note that you need to #include <limits> and <ios> for numeric_limits and streamsize. You can also just use some big number inside the ignore like this:
Code:
`    std::cin.ignore(1000, '\n'); // ignore bad input`

5. Originally Posted by Daved
This is the way I like to recommend. It evaluates the return value of the input, which will be false if the user enters something that is not a number. It can be expanded to fail if the user enters a number followed by a letter if you want to do that (e.g. you want to fail on 123abc), just ask or search.[code]int number = 0;
But it sounds like he wants to determine if a number is a non-integer, not whether the input is a number.

6. Where is the number coming from?

If the number is coming from user input, then what Daved said works best to ensure that the number is an int.

Alternatively, if you still want to accept floating point values but do something different on then than integer values, you can use code like brewbuck suggested, except that you also have to do a seperate test for values outside the int range. I believe fmod() in cmath can be used for this.

If the number is a result of some floating point computation, you need a more complex approach. You need to test if the value is reasonably close to an integer. Because of rounding errors, a result that should be an integer may actually come out a few small fractions smaller. Because of this, simply doing what brewbuck suggested will not work.

7. >> But it sounds like he wants to determine if a number is a non-integer, not whether the input is a number.

Yeah, I wasn't sure which one was wanted. Either way this code will work for both:
Code:
```int number = 0;

// ask user for number here
while (!(std::cin >> number) || std::cin.get() != '\n')
{
std::cin.clear(); // clear fail state
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // ignore bad input
// warn user and re-prompt here
}```