Check if input is only numbers

Discussion in 'C++' started by eli m, Mar 9, 2013.

  1. eli m

    eli m Guest

    In my program i ask for input and then i want it to display a certain message when they enter anything except numbers. How would i do this?
     
    eli m, Mar 9, 2013
    #1
    1. Advertising

  2. eli m

    Stefan Ram Guest

    eli m <> writes:
    >In my program i ask for input and then i want it to display a
    >certain message when they enter anything except numbers. How
    >would i do this?


    I think, recently, someone posted this:

    std::string line;
    int lineNumber = 0;
    while ( std::getline( file, line ) ) {
    ++ lineNumber;
    std::istringstream parser( line );
    if ( parser >> i >> j >> k ) { // For example...
    // ...
    }
    }
     
    Stefan Ram, Mar 9, 2013
    #2
    1. Advertising

  3. eli m

    Bill Gill Guest

    On 3/9/2013 3:39 PM, eli m wrote:
    > In my program i ask for input and then i want it to display a certain message when they enter anything except numbers. How would i do this?
    >

    The way I have been taught is:
    int i = 0;
    if (!cin >> i)
    cout << "error";

    Bill
     
    Bill Gill, Mar 9, 2013
    #3
  4. eli m

    Öö Tiib Guest

    On Saturday, 9 March 2013 23:39:17 UTC+2, eli m wrote:
    > In my program i ask for input and then i want it to display a certain
    > message when they enter anything except numbers. How would i do this?


    Lot of ways

    * Check that none of characters that were input is neither less than '0'
    nor greater than '9'.
    * Use boost lexical_cast.
    * Use strtol() and detect how far it got in string.

    What have you tried?
     
    Öö Tiib, Mar 10, 2013
    #4
  5. eli m

    Stefan Ram Guest

    Juha Nieminen <> writes:
    >In general, parsing textual input is not one of the easiest things to
    >do in C++.


    To parse an input, one first need to know its grammar. Once
    the grammar is written, writing a parser is straightforward.

    However, when a programmer who is not even capable of
    writing the grammar of his input language attempts to write
    a parser for it, something is wrong!

    There is nothing wrong with not being able to write a
    grammar. One just should not attempt to write a parser in
    this case. So the blame could be on learners who learn in an
    uncoordinated manner or on the teachers who expect them to
    write programs reading input without proper preparation.

    Even if C++ would provide more support for parsing (for
    example, with libraries like YACC or LAX), one still would
    need to know the grammar to be used.
     
    Stefan Ram, Mar 10, 2013
    #5
  6. eli m

    Jorgen Grahn Guest

    On Sun, 2013-03-10, Stefan Ram wrote:
    > Juha Nieminen <> writes:
    >>In general, parsing textual input is not one of the easiest things to
    >>do in C++.

    >
    > To parse an input, one first need to know its grammar. Once
    > the grammar is written, writing a parser is straightforward.

    ....
    > Even if C++ would provide more support for parsing (for
    > example, with libraries like YACC or LAX), one still would
    > need to know the grammar to be used.


    (You mean lex, not LAX, I think.)

    Actually C++ /does/ support parsing today, via its regex support.
    And you're right of course: the language cannot do your interface
    design for you.

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
     
    Jorgen Grahn, Mar 10, 2013
    #6
  7. eli m

    Öö Tiib Guest

    On Sunday, 10 March 2013 18:32:21 UTC+2, Jorgen Grahn wrote:
    > On Sun, 2013-03-10, Stefan Ram wrote:
    > > Juha Nieminen <> writes:
    > >>In general, parsing textual input is not one of the easiest things to
    > >>do in C++.

    > > To parse an input, one first need to know its grammar. Once
    > > the grammar is written, writing a parser is straightforward.

    > ...
    > > Even if C++ would provide more support for parsing (for
    > > example, with libraries like YACC or LAX), one still would
    > > need to know the grammar to be used.

    >
    > (You mean lex, not LAX, I think.)
    >
    > Actually C++ /does/ support parsing today, via its regex support.
    > And you're right of course: the language cannot do your interface
    > design for you.


    The regex comes into play rather soon in well-designed interactive
    UI since most modern users can't type anymore. As soon they type
    few characters they expect a list of hints.
     
    Öö Tiib, Mar 10, 2013
    #7
  8. eli m

    Öö Tiib Guest

    On Tuesday, 12 March 2013 10:05:42 UTC+2, Juha Nieminen wrote:
    > Jorgen Grahn <> wrote:
    > > Actually C++ /does/ support parsing today, via its regex support.

    >
    > Regular expressions are best for matching and replacing. They are a rather
    > poor tool for parsing. You could try to parse some constructs with regular
    > expressions, but many such constructs become cumbersome, if not even
    > impossible, to parse with regexes. (Regular expressions have their limits
    > on what they support. For example, it's not possible to write a regex that
    > says "a substring with as many '(' characters as ')' characters", which
    > could be used to parse expressions with nested parentheses.)


    Regex is limited and that is good. It is easy to match simple commands
    and recognize wildcard characters with it. OP's 'numbers only' are easy
    to match with regex ("+[:digit:]" or "+[0-9]").

    Regex should not carry parsing scripting or markup language. When parsing
    is complex then it is usually most sane to integrate existing script
    language (with something like boost::python or luajit) or existing
    markup language (with things like libjson or RapidXML). "Not Invented
    Here" guys do not get too far in our industry anymore.

    That leaves only odd cases between the two that are difficult with regex
    .... yet do not qualify as language or when it is a language without
    suitable parsing library. Firing up boost::spirit or YACC/LEX for those
    limited cases is not that difficult.
     
    Öö Tiib, Mar 12, 2013
    #8
  9. eli m

    Jorgen Grahn Guest

    On Tue, 2013-03-12, Juha Nieminen wrote:
    > Jorgen Grahn <> wrote:
    >> Actually C++ /does/ support parsing today, via its regex support.

    >
    > Regular expressions are best for matching and replacing. They are a rather
    > poor tool for parsing.


    True, they don't replace e.g. yacc or even lex. But for things like
    "Check if input is only numbers" they are useful (or would be, if C++
    didn't have strtol(3) and friends already).

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
     
    Jorgen Grahn, Mar 12, 2013
    #9
  10. eli m

    James Kanze Guest

    On Saturday, 9 March 2013 23:33:13 UTC, Bill Gill wrote:
    > On 3/9/2013 3:39 PM, eli m wrote:


    > > In my program i ask for input and then i want it to display
    > > a certain message when they enter anything except numbers.
    > > How would i do this?


    > The way I have been taught is:


    > int i = 0;
    > if (!cin >> i)
    > cout << "error";


    Certainly not. That's "(!cin) >> i" in the if, and it shouldn't
    even compile.

    --
    James
     
    James Kanze, Mar 12, 2013
    #10
  11. eli m

    James Kanze Guest

    On Tuesday, 12 March 2013 08:05:42 UTC, Juha Nieminen wrote:
    > Jorgen Grahn <> wrote:


    > > Actually C++ /does/ support parsing today, via its regex support.


    > Regular expressions are best for matching and replacing. They are a rather
    > poor tool for parsing. You could try to parse some constructs with regular
    > expressions, but many such constructs become cumbersome, if not even
    > impossible, to parse with regexes. (Regular expressions have their limits
    > on what they support. For example, it's not possible to write a regex that
    > says "a substring with as many '(' characters as ')' characters", which
    > could be used to parse expressions with nested parentheses.)


    Every parser I've ever written has used regular expressions for
    the lexical analysis. That's really what regular expressions
    where designed for. For full parsing, of course: regular
    expressions can only define a type 3 grammar. For a full
    context free grammar (type 2) you'll need more.

    If the problem is just to know whether the file contains only
    numbers, you don't need more than a type 1 grammar, so regular
    expressions are fine.

    --
    James
     
    James Kanze, Mar 12, 2013
    #11
  12. eli m

    James Kanze Guest

    On Tuesday, 12 March 2013 12:00:34 UTC, Jorgen Grahn wrote:
    > On Tue, 2013-03-12, Juha Nieminen wrote:
    > > Jorgen Grahn <> wrote:


    > >> Actually C++ /does/ support parsing today, via its regex support.


    > > Regular expressions are best for matching and replacing. They are a rather
    > > poor tool for parsing.


    > True, they don't replace e.g. yacc or even lex.


    Lex *is* regular expressions.

    --
    James
     
    James Kanze, Mar 12, 2013
    #12
  13. eli m

    Jorgen Grahn Guest

    On Tue, 2013-03-12, James Kanze wrote:
    > On Tuesday, 12 March 2013 12:00:34 UTC, Jorgen Grahn wrote:
    >> On Tue, 2013-03-12, Juha Nieminen wrote:
    >> > Jorgen Grahn <> wrote:

    >
    >> >> Actually C++ /does/ support parsing today, via its regex support.

    >
    >> > Regular expressions are best for matching and replacing. They are a rather
    >> > poor tool for parsing.

    >
    >> True, they don't replace e.g. yacc or even lex.

    >
    > Lex *is* regular expressions.


    It's tokenization driven by regular expressions. I don't think lex is
    a replacement for std::regex (or whatever its name is) or the other
    way around. (But I haven't used lex for at least a decade.)

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
     
    Jorgen Grahn, Mar 12, 2013
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mit
    Replies:
    1
    Views:
    838
    Ramu Pulipati
    Jan 25, 2006
  2. Guy
    Replies:
    5
    Views:
    2,862
    brucie
    Dec 13, 2003
  3. Subra
    Replies:
    25
    Views:
    1,247
    user923005
    Mar 8, 2007
  4. Andrew Tatum

    Fibonacci Numbers and Lucas Numbers

    Andrew Tatum, May 26, 2007, in forum: C++
    Replies:
    6
    Views:
    593
    Howard
    May 27, 2007
  5. PJ

    allow only numbers in an input

    PJ, Dec 19, 2005, in forum: Javascript
    Replies:
    2
    Views:
    134
Loading...

Share This Page