Console Number Exception Handling.

Discussion in 'C++' started by Ed Dana, Oct 10, 2008.

  1. Ed Dana

    Ed Dana Guest

    I am attempting to use exception handling in the console to trap an
    invalid character into a numeric field. Unfortunately, using this example:
    ======================================================================
    #include <exception>

    #include <iostream>
    using namespace std;

    int main(int argc, char *argv[]) {
    double number = 0;

    while (number <= 0) {
    std::cout << "Gimme a number: ";
    try {
    std::cin >> number;
    }
    catch (exception e) {
    std::cout << "Oops, you did it again.";
    }
    }

    }
    ======================================================================
    all I get is an endless loop. What exception should I be using and what
    header file if not above. Any clues appreciated.
     
    Ed Dana, Oct 10, 2008
    #1
    1. Advertising

  2. Ed Dana

    Barry Guest

    On Oct 11, 6:46 am, Ed Dana <> wrote:
    > I am attempting to use exception handling in the console to trap an
    > invalid character into a numeric field. Unfortunately, using this example:
    > ======================================================================
    > #include <exception>
    >
    > #include <iostream>
    > using namespace std;
    >
    > int main(int argc, char *argv[]) {
    >    double number = 0;
    >
    >    while (number <= 0) {
    >      std::cout << "Gimme a number: ";
    >      try {
    >        std::cin >> number;
    >      }
    >      catch (exception e) {
    >        std::cout << "Oops, you did it again.";
    >      }
    >    }
    >
    > }
    >
    > ======================================================================
    > all I get is an endless loop. What exception should I be using and what
    > header file if not above. Any clues appreciated.


    something like this:
    http://www.parashift.com/c -faq-lite/input-output.html#faq-15.5

    --
    Best Regards
    Barry
     
    Barry, Oct 11, 2008
    #2
    1. Advertising

  3. Ed Dana

    Ed Dana Guest

    Ed Dana, Oct 11, 2008
    #3
  4. Ed Dana

    Rolf Magnus Guest

    Rolf Magnus, Oct 11, 2008
    #4
  5. Ed Dana

    Ed Dana Guest

    Rolf Magnus wrote:
    >
    > Btw, you can use exceptions for this if you like, but you must enabled them
    > explicitly first.


    Any examples of this would be appreciated. :)

    Ed.
     
    Ed Dana, Oct 12, 2008
    #5
    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. Ben Finney
    Replies:
    2
    Views:
    1,016
    Egor Bolonev
    Jun 30, 2003
  2. Mr Major Thorburn

    Exception handling source line number

    Mr Major Thorburn, Mar 12, 2008, in forum: ASP .Net
    Replies:
    2
    Views:
    547
    Mr Major Thorburn
    Mar 12, 2008
  3. Peter
    Replies:
    34
    Views:
    1,995
    James Kanze
    Oct 17, 2009
  4. VSK
    Replies:
    0
    Views:
    260
  5. Mr Major Thorburn

    Exception handling source line number

    Mr Major Thorburn, Mar 12, 2008, in forum: ASP General
    Replies:
    1
    Views:
    244
    Bob Barrows [MVP]
    Mar 12, 2008
Loading...

Share This Page