Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Console Number Exception Handling.

Reply
Thread Tools

Console Number Exception Handling.

 
 
Ed Dana
Guest
Posts: n/a
 
      10-10-2008
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.
 
Reply With Quote
 
 
 
 
Barry
Guest
Posts: n/a
 
      10-11-2008
On Oct 11, 6:46*am, Ed Dana <(E-Mail Removed)> 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-lit....html#faq-15.5

--
Best Regards
Barry
 
Reply With Quote
 
 
 
 
Ed Dana
Guest
Posts: n/a
 
      10-11-2008
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-lit....html#faq-15.5
>
> --
> Best Regards
> Barry

 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      10-11-2008
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-lit....html#faq-15.5


Btw, you can use exceptions for this if you like, but you must enabled them
explicitly first.
 
Reply With Quote
 
Ed Dana
Guest
Posts: n/a
 
      10-12-2008
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Exception of type 'System.Web.HttpUnhandledException' wasthrown.Exception has been thrown by the target of an invocation.System.WebSystem.Exception jobs ASP .Net 1 11-16-2007 05:57 PM
while executing my client program i get the exception javax.naming.LinkException: [Root exception is javax.naming.LinkException: [Root exception is javax.naming.NameNotFoundException: remaining if plz anybody know how to solve this problem then mahesh Java 0 03-08-2007 12:26 PM
how to show C line number AND exception type in JNI exception stacktrace? Elaine Cheong Java 1 12-09-2006 08:59 AM
Re: console mp3->wav decoder for $indows or GUI one which supports console Egor Bolonev Python 0 06-30-2003 10:43 AM
Re: console mp3->wav decoder for $indows or GUI one which supports console Ben Finney Python 2 06-30-2003 05:43 AM



Advertisments