[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Console Number Exception Handling.

Ed Dana

10/10/2008 10:46:00 PM

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.
4 Answers

Barry

10/11/2008 1:01:00 AM

0

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

--
Best Regards
Barry

Ed Dana

10/11/2008 2:13:00 AM

0

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

Barry wrote:
>
> something like this:
> http://www.parashift.com/c++-faq-lite/input-output.htm...
>
> --
> Best Regards
> Barry

Rolf Magnus

10/11/2008 8:10:00 AM

0

Ed Dana wrote:

> Thanks. That was definitely the clue I needed. (Certainly not the one I
> expected.)
>
> Barry wrote:
>>
>> something like this:
>> http://www.parashift.com/c++-faq-lite/input-output.htm...

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

Ed Dana

10/12/2008 3:17:00 PM

0

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.