checking for non-integer values

This is a discussion on checking for non-integer values within the C++ Programming forums, part of the General Programming Boards category; Hi! I am working on a program to add, subtract, multiply and divide fractions and I need some way to ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    16

    Question 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. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Something akin to this?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,244
    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. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    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. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,244
    Quote Originally Posted by Daved View Post
    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. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,041
    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.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    >> 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
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overflow and range checking for mul/div
    By Elysia in forum C++ Programming
    Replies: 28
    Last Post: 06-06-2008, 02:09 PM
  2. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  3. Operator Overloading (Bug, or error in code?)
    By QuietWhistler in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2006, 07:38 AM
  4. newbie programmer - needs help bad.
    By hortonheat in forum C Programming
    Replies: 17
    Last Post: 10-20-2004, 05:31 PM
  5. checking if a char * is an integer
    By gregulator in forum C Programming
    Replies: 5
    Last Post: 04-16-2004, 09:12 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21