handle cin errors

Discussion in 'C++' started by Nafai, Mar 23, 2005.

  1. Nafai

    Nafai Guest

    Hello. I need to handle cin errors like these:

    ....
    int n;
    cout << "Type a number: ";
    cin >> n;
    // Check user actually typed a number.
    ....


    CONSOLE:

    Type a number: asdf >ENTER
    ....
    Programs does strange things
    ....


    How can I do that?

    Thanks!
     
    Nafai, Mar 23, 2005
    #1
    1. Advertising

  2. Nafai wrote:
    > Hello. I need to handle cin errors like these:
    >
    > ...
    > int n;
    > cout << "Type a number: ";
    > cin >> n;
    > // Check user actually typed a number.
    > ...
    >
    >
    > CONSOLE:
    >
    > Type a number: asdf >ENTER
    > ...
    > Programs does strange things
    > ...
    >
    >
    > How can I do that?


    Read the FAQ (http://www.parashift.com/c -faq-lite/), your question
    and an answer has been put there.

    V
     
    Victor Bazarov, Mar 23, 2005
    #2
    1. Advertising

  3. Victor Bazarov wrote:

    > Nafai wrote:
    >
    >> Hello. I need to handle cin errors like these:
    >>
    >> ...
    >> int n;
    >> cout << "Type a number: ";
    >> cin >> n;
    >> // Check user actually typed a number.
    >> ...
    >>
    >> CONSOLE:
    >>
    >> Type a number: asdf >ENTER
    >> ...
    >> Programs does strange things
    >> ...
    >>
    >> How can I do that?

    >
    > Read the FAQ (http://www.parashift.com/c -faq-lite/),
    > your question and an answer has been put there.


    [15.3] How can I get std::cin to skip invalid input characters?
     
    E. Robert Tisdale, Mar 23, 2005
    #3
  4. E. Robert Tisdale wrote:
    > Victor Bazarov wrote:
    >
    >> Nafai wrote:
    >>
    >>> Hello. I need to handle cin errors like these:
    >>>
    >>> ...
    >>> int n;
    >>> cout << "Type a number: ";
    >>> cin >> n;
    >>> // Check user actually typed a number.
    >>> ...
    >>>
    >>> CONSOLE:
    >>>
    >>> Type a number: asdf >ENTER
    >>> ...
    >>> Programs does strange things
    >>> ...
    >>>
    >>> How can I do that?

    >>
    >>
    >> Read the FAQ (http://www.parashift.com/c -faq-lite/), your question
    >> and an answer has been put there.

    >
    >
    > [15.3] How can I get std::cin to skip invalid input characters?


    [5.5] What do I do if someone else posts a question that's already
    in the FAQ?

    And see note #1.

    V
     
    Victor Bazarov, Mar 23, 2005
    #4
  5. What do I do if someone else posts a question that's already in theFAQ?

    Victor Bazarov wrote:

    > E. Robert Tisdale wrote:
    >
    >> Victor Bazarov wrote:
    >>
    >>> Read the FAQ (http://www.parashift.com/c -faq-lite/),
    >>> your question and an answer has been put there.


    I couldn't tell which FAQ you were talking about
    and I don't think that Nafai could either.

    >> [15.3] How can I get std::cin to skip invalid input characters?

    >
    > [5.5] What do I do if someone else posts a question
    > that's already in the FAQ?
    >
    > And see note #1.


    Note #1: Please don't give them the location
    of the appropriate FAQ. E.g., don't say,
    "Look at FAQ [10.3]" or "Look in section [10]".
    It's the old give-them-a-fish vs. teach-them-to-fish problem.

    This is bad advice. It contradicts

    Note #2: Please be polite. I'm hoping we can avoid
    "RTFM" or "RTFFAQ" euphemisms (or worse!).

    Please *don't* send new subscribers on a "wild goose chase".
    Cite and quote the relevant FAQ
    just as if you were citing and/or quoting a relevant passage
    from the ANSI/ISO C++ standards documents.
     
    E. Robert Tisdale, Mar 23, 2005
    #5
  6. Re: What do I do if someone else posts a question that's already in the FAQ?

    "E. Robert Tisdale" <> wrote...
    > [...]
    > This is bad advice. It contradicts


    When was it you admitted that anybody else's advice was good?
    Stop trolling.
     
    Victor Bazarov, Mar 24, 2005
    #6
    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. Mark Goldin

    Errors, errors, errors

    Mark Goldin, Jan 17, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    985
    Mark Goldin
    Jan 17, 2004
  2. TaiwanNoWhere

    cin and cin.clear() problem

    TaiwanNoWhere, Oct 13, 2003, in forum: C++
    Replies:
    8
    Views:
    4,114
    P.J. Plauger
    Oct 17, 2003
  3. Chris Mantoulidis

    std::cin.ignore() and std::cin.clear()

    Chris Mantoulidis, Jan 6, 2004, in forum: C++
    Replies:
    5
    Views:
    17,202
    Kevin Saff
    Jan 6, 2004
  4. Aleander

    cin and cin.getline()

    Aleander, Mar 6, 2005, in forum: C++
    Replies:
    5
    Views:
    8,745
    Alex Vinokur
    Mar 6, 2005
  5. Fernando
    Replies:
    4
    Views:
    1,744
    Fernando
    Nov 16, 2011
Loading...

Share This Page