Thread: Checking if input is a number

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    43

    Checking if input is a number

    Hello everyone.

    I am a complete begginer to C++. I started from scratch yesterday with the cprogramming tutorials, which are very helpful by the way.

    I must ask you to bare with me with this question, since the only language i've learned so far is Visual Basic, so my thinking process isn't yet set to understand C++.

    Imagine I'm writting a programme that allows me to convert temperature units. I'd use cin>>variable; to atributte a value to later be converted.

    What happens if i enter 'a' instead of '100'? I'd like to reject any input that isn't numeric.

    Thanks in advance.

  2. #2
    Registered User
    Join Date
    Sep 2012
    Posts
    43
    The variable would be of type double

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You could do something like:
    Code:
    if (cin >> variable)
    {
        // Process variable.
    }
    else
    {
        // Inform user of an input error.
    }
    More generally, you can read the input as a string then parse the string.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    43
    Take this simple code

    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
        int var;
        while (1)
        {
              cin>>var;
              cout<<var;
        }
    }
    var is an integer, so when i input '1' it writes '1' afterwards, but if i write 'a', it goes on writting some number i dont understand. It does not break with an error or anything.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    So, how would you incorporate or otherwise make use of my suggestion in post #3?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    As laserlight suggested, use a string to input the values. They won't goof up if you enter the wrong thing.
    Then you have to check each character in the string to make sure it's a valid number.
    When you've concluded it's valid, convert the string to a number and continue, otherwise, scream at the user...

    If you're really new, you might want to get a little farther along before tackling this type of verification. It can be complex. A couple more weeks might give you the background to do this easily.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by WaltP View Post
    As laserlight suggested, use a string to input the values. They won't goof up if you enter the wrong thing.
    Then you have to check each character in the string to make sure it's a valid number.
    When you've concluded it's valid, convert the string to a number and continue, otherwise, scream at the user...

    If you're really new, you might want to get a little farther along before tackling this type of verification. It can be complex. A couple more weeks might give you the background to do this easily.
    Laserlight did not suggest such a thing.
    That is one option, but the easy option is to just check the state of the stream after the read, which is what Laserlight's code does. The stream is implicitly convertible to bool. You still need to reset the stream back into a good state if the read fails though.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    He didn't?

    Quote Originally Posted by laserlight View Post
    More generally, you can read the input as a string then parse the string.
    Yes he did. I just expanded on how...
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Whoops, you're right... brainfart.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  10. #10
    Registered User
    Join Date
    Sep 2012
    Posts
    43
    To be honest, I didn't understand any of the suggestions you guys made. I'll follow WaltP's advice and keep on learning for a couple of weeks before tackling this issue. I honestly thought it would be much more straightforward :P

    In my (little) programming experience, with MatLab and Visual Basic, if I tried to input a string into a double type variable, the programme would stop with an error. In C++ it doesn't happen and I still don't understand why. I guess I don't yet understand how storing different type of data into variables really work.

  11. #11
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    It's because when C/C++ attempts to read a number and gets a non-number, the input functions stop reading at that point.
    The offending character stays in the buffer.
    Next read the character is still there.
    Next read the character is still there.
    Next read the character is still there.
    Next read -- ad nauseum.

    You have to catch the error and clear out the buffer.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. checking if number is prime
    By jackson6612 in forum C++ Programming
    Replies: 4
    Last Post: 04-30-2011, 03:02 PM
  2. Input a Hex number and output hex number to a text field
    By zoobaby in forum C++ Programming
    Replies: 4
    Last Post: 05-12-2009, 11:26 AM
  3. Checking for a number
    By a.mlw.walker in forum C Programming
    Replies: 7
    Last Post: 04-20-2009, 03:42 PM
  4. checking for whole number
    By Dummies102 in forum C++ Programming
    Replies: 2
    Last Post: 02-26-2002, 04:55 PM
  5. checking if input is a number ?
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 12-06-2001, 09:07 PM

Tags for this Thread