How to catch this exception? 
Author Message
 How to catch this exception?

Hi all,

the question is: when i use scanf("%d", &label) how can i catch
the exception if somebody puts a letter from keyboard?

thank you
--
Lord Gas, the Waxen - Blasfemo di IFTMDG - Penguin's Enemy #1
[ICQ: 57483751][the Waxen Pith: http://www.*-*-*.com/ ]
... "Goddamn, there's always such a big temptation to be good"
... (Tom Waits - God's Away on Business)
--



Sun, 18 Dec 2005 19:20:18 GMT  
 How to catch this exception?

Quote:
> the question is: when i use scanf("%d", &label) how can i catch
> the exception if somebody puts a letter from keyboard?

You look at the return value of scanf().  You really should have found
that in whatever C textbook you're learning from, or the FAQ, before
posting this question.
--

Even if all the snow were burnt, ashes would remain.
--



Tue, 20 Dec 2005 07:37:42 GMT  
 How to catch this exception?

Quote:
> the question is: when i use scanf("%d", &label) how can i catch
> the exception if somebody puts a letter from keyboard?

Hint:  What does scanf return?

By the way, nothing will be scanned (on some systems) until after
a new-line character has been input.  You might want to rethink
the interface.
--



Tue, 20 Dec 2005 07:38:10 GMT  
 How to catch this exception?
Hello,


Quote:
> the question is: when i use scanf("%d", &label) how can i catch
> the exception if somebody puts a letter from keyboard?

Check the return-value of scanf(). Don't forget to read away the
unwanted non-numeric characters in this case (because scanf() does
not read them, so they will still be in the input stream when you
read the next time ...).

bye,

--
Das Netz ist Freude. Es ist Ekstase, die jeden einzelnen Nerv erglhen
l??t. Es ist Duft, den man fhlt. Es ist ein Bild, das man riecht.
Es ist Erfllung - ein Geschmack, neben dem alles andere schal ist.    
("Netzreiter-Preisung" aus dem Buch "Der Netzparasit" von Andreas Brandhorst)
--



Tue, 20 Dec 2005 07:38:16 GMT  
 How to catch this exception?

Quote:
> Hi all,

> the question is: when i use scanf("%d", &label) how can i catch
> the exception if somebody puts a letter from keyboard?

It's not an exception.

The function scanf() returns the number of conversions done; if, as in
your example, a letter is input instead of a number, it will return 0.

HTH,
--ag

--
Artie Gold -- Austin, Texas
--



Tue, 20 Dec 2005 07:38:25 GMT  
 How to catch this exception?


Quote:
> Hi all,

> the question is: when i use scanf("%d", &label) how can i catch
> the exception if somebody puts a letter from keyboard?

No exception will be given. scanf() stops after the first invalid character,
and returns the number of characters actually scanned. Your only way to
detect a ad character using this code is if scanf() returns 0.

A better solution for data validation relies on reading characters one by
one,  and performing the appropriate validation in your code. Note that you
will probably have to use machine-dependent functions in order to handle
issues such as erasing characters, etc.

HTH,
Daniel Pfeffer
--



Tue, 20 Dec 2005 07:38:27 GMT  
 How to catch this exception?


# Hi all,
#
# the question is: when i use scanf("%d", &label) how can i catch
# the exception if somebody puts a letter from keyboard?

Read about the return value that scanf yields. Should be obvious.

Regards,

        Jens
--
Jens Schweikhardt http://www.schweikhardt.net/
SIGSIG -- signature too long (core dumped)
--



Tue, 20 Dec 2005 07:38:29 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Q: Why try catch cannot catch the exception

2. Why VC++ does not caught this exception?

3. Howto catch Win32 exceptions in C++?

4. Getting information in a catch-all exception

5. How to catch all exceptions?

6. How to catch all exceptions thrown?

7. How to catch an Exception ?

8. try-catch w/ CException doesn't catch all exceptions?

9. How can I catch the exceptions error in ANSI C?

10. How to catch the exception and deal with it?

11. Catching Win32 exceptions with a C++ catch-block

12. Catching hardware exceptions using C++ exceptions

 

 
Powered by phpBB® Forum Software