# Thread: How to check if an entered number is an integer or not

1. ## How to check if an entered number is an integer or not

The short version: How can I determine if a value entered by a user and assigned to some variable is an integer value or a double?

The explanation:
This question is related to a homework problem, though not part of the homework.

My assignment is to write a program that asks the user to enter an integer- suppose for now that it's 2- and then uses it to compute a value with this given formula: ((x/3) + (x%3)) * 3.

Then I must output the entire formula and the answer in this format:
"((2/3) + (2%3)) * 3 = 2.000".

This is very basic, I know, but it's the first week of class and it's a beginner's class.

Now, I have finished this assignment already, but something occurred to me: What if the user enters a non-integer value?

I started to plan the program in pseudo-code using an IF-ELSE structure and a WHILE structure to see which makes more sense.
Something like:
IF (the value entered is an integer), then do the calculation.
ELSE, tell the user to try again.

Immediately I ran into the problem of how to determine if the already entered value is an integer or not.

I have looked online and on other fora but no one seems to have a definite answer to this very simple question. And all the answers I've seen involve things that are way beyond my current knowledge.

So, again, a user enters a value and some variable is assigned that value. How do I then determine if they entered an integer or not?

2. Well if you have an integer variable

Code:
```#include <iostream>
using namespace std;
int main ( ) {
int var;
if ( cin >> var ) {
cout << "Success " << var << endl;
} else {
char b[10000];
if ( cin.good() ) {
cin.getline(b,sizeof(b));
cout << "b=" << b << endl;
} else if ( cin.bad() ) {
cout << "Something bad" << endl;
} else if ( cin.fail() ) {
cout << "Something failed" << endl;
} else if ( cin.eof() ) {
cout << "EOF" << endl;
}
}
}```
If you type in 123, then var is 123 and you run success
if you type in 123abc, then var is 123 and you run success, and abc is there for next time
if you type in abc123, then it's just oops, and abc123 is still in the input buffer waiting

3. Basically,
when you use std::cin >> var, the stream will enter an error state if the user did enter something that can be converted to var's type.
To detect this, basically you'd do if (std::cin >> var).
Then you know if it's an integer or not (if var's type is an integer).
If you want to distinguish between an int and a double, it's a little more difficult, but one way to do it is to read into a double, then do (assuming var is a double)

if (var == ceil(var))
// It's an integer
else
// It's not an integer

Basically, we can abuse the fact that an integer has no fractional part, and since ceil round a floating number up to nearest integer, if there is no rounding, then it must be an integer. Otherwise a float.

Now, if the stream goes into an error state, you need to clear the error state and flush the input buffer.
This will probably help: c++ - How do I flush the cin buffer? - Stack Overflow (see replies 2 [flushing input buffer] & 3 [example; the flushing part is wrong so don't use it, though]).

4. Originally Posted by hinc
The short version: How can I determine if a value entered by a user and assigned to some variable is an integer value or a double?
Well, if you stored it in an integer variable then it's an integer. If you stored it in a double then it's a double.

If you want to check if the value stored in a double is actually an integer, you can do that. But that's not the same thing as ensuring that the user entered an integer. For instance, the user might enter 1.000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000001, which is not an integer, but will be rounded to the exact value "1" because a double doesn't have enough precision to express that.

Popular pages Recent additions