Console Number Exception Handling.

E

Ed Dana

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.
 
B

Barry

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
 
R

Rolf Magnus

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

Btw, you can use exceptions for this if you like, but you must enabled them
explicitly first.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top