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

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    1

    Question 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. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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]).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by hinc View Post
    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.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Check entered address range is valid
    By mrapoc in forum C Programming
    Replies: 4
    Last Post: 08-04-2012, 07:10 AM
  2. Check wheather the entered number is prime also repeat it
    By rajesh10071986 in forum C Programming
    Replies: 6
    Last Post: 01-11-2010, 04:34 AM
  3. Replies: 7
    Last Post: 04-12-2009, 12:40 PM
  4. How do you check how many characters a user has entered?
    By engstudent363 in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 06:05 AM
  5. How do I check data entered and generate an error
    By KenBuckner in forum C Programming
    Replies: 1
    Last Post: 05-16-2004, 11:06 PM

Tags for this Thread