Console Number Exception Handling.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ed Dana

    Console Number Exception Handling.

    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.
  • Barry

    #2
    Re: Console Number Exception Handling.

    On Oct 11, 6:46 am, Ed Dana <EDan...@Cox.ne twrote:
    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:


    --
    Best Regards
    Barry

    Comment

    • Ed Dana

      #3
      Re: Console Number Exception Handling.

      Thanks. That was definitely the clue I needed. (Certainly not the one I
      expected.)

      Barry wrote:

      Comment

      • Rolf Magnus

        #4
        Re: Console Number Exception Handling.

        Ed Dana wrote:
        Thanks. That was definitely the clue I needed. (Certainly not the one I
        expected.)
        >
        Barry wrote:
        Btw, you can use exceptions for this if you like, but you must enabled them
        explicitly first.

        Comment

        • Ed Dana

          #5
          Re: Console Number Exception Handling.

          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.

          Comment

          Working...