Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > How to catch this exception?

Reply
Thread Tools

How to catch this exception?

 
 
Lord Gas, the Waxen
Guest
Posts: n/a
 
      07-01-2003
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.snipp.org/gas]
.... "Goddamn, there's always such a big temptation to be good"
.... (Tom Waits - God's Away on Business)
 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      07-01-2003
"Lord Gas, the Waxen" <(E-Mail Removed)> wrote in
news:(E-Mail Removed):

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


What exception?

Have you looked into what scanf returns?

--
A. Sinan Unur
http://www.velocityreviews.com/forums/(E-Mail Removed)
Remove dashes for address
Spam bait: (E-Mail Removed)
 
Reply With Quote
 
 
 
 
Lord Gas, the Waxen
Guest
Posts: n/a
 
      07-01-2003
Greetings, you horrible lot. Here is the last warp from A. Sinan
Unur
> "Lord Gas, the Waxen" <(E-Mail Removed)> wrote in
> news:(E-Mail Removed):
>
> > Hi all,
> >
> > the question is: when i use scanf("%d", &label) how can i catch
> > the exception if somebody puts a letter from keyboard?

>
> What exception?
> Have you looked into what scanf returns?


it returns the number of variables... but, i mean, %d means
decimal, so i have to put a number... my program crushes if i put
something different from a number. Look, this is the code:

while(1){
printf("\nPress '1' to read messages\nPress '2' to write a
message\nPress '3' to delete a message\nPress any other key to
exit\n");
scanf("%d",&scelta);
switch(scelta){
case 1: reader(); break;
case 2: writer(); break;
case 3: rubber(); break;
default: ExitProcess(0);
}
}

if i put a letter, it enters an infinite loop instead of being
terminated...

what's the matter with it?

Thanx for the answer

--
Lord Gas, the Waxen - Blasfemo di IFTMDG - Penguin's Enemy #1
[ICQ: 57483751][the Waxen Pith: http://www.snipp.org/gas]
.... "Goddamn, there's always such a big temptation to be good"
.... (Tom Waits - God's Away on Business)
 
Reply With Quote
 
Lord Gas, the Waxen
Guest
Posts: n/a
 
      07-01-2003
Greetings, you horrible lot. Here is the last warp from Tom St
Denis

> Which is why you should use fgets/sscanf.


i did as you told, but it doesn't work ((
maybe what you said is right, but i'm not able to use it in the
right way... i'm very green with C...

any other suggestion?

thanx for your patience...

--
Lord Gas, the Waxen - Blasfemo di IFTMDG - Penguin's Enemy #1
[ICQ: 57483751][the Waxen Pith: http://www.snipp.org/gas]
.... "Goddamn, there's always such a big temptation to be good"
.... (Tom Waits - God's Away on Business)
 
Reply With Quote
 
Arthur J. O'Dwyer
Guest
Posts: n/a
 
      07-01-2003

On Tue, 1 Jul 2003, Lord Gas, the Waxen wrote:
>
> Greetings, you horrible lot. Here is the last warp from A. Sinan Unur
> > "Lord Gas, the Waxen" <(E-Mail Removed)> wrote...
> > >
> > > the question is: when i use scanf("%d", &label) how can i catch
> > > the exception if somebody puts a letter from keyboard?

> >
> > What exception?
> > Have you looked into what scanf returns?

>
> it returns the number of variables... but, i mean, %d means
> decimal, so i have to put a number... my program crushes if i put
> something different from a number. Look, this is the code:


Does your *code* check what scanf returns?

Try assigning the return value of scanf to a variable, and
then using one of the relational operators to test the value
of that variable against an integer constant (e.g., the number 1).

Advanced users can even skip the assigning-to-variable step,
if they like.

> while(1){
> printf("\nPress '1' to read messages\nPress '2' to write a
> message\nPress '3' to delete a message\nPress any other key to
> exit\n");
> scanf("%d",&scelta);


Here you call scanf. Where do you check the return value?

> if i put a letter, it enters an infinite loop instead of being
> terminated...
>
> what's the matter with it?


You already got a very good answer. I'm merely acting as the
mirror here.

-Arthur
 
Reply With Quote
 
Lord Gas, the Waxen
Guest
Posts: n/a
 
      07-01-2003
Greetings, you horrible lot. Here is the last warp from Tom St
Denis

> simple code

[cut]
> Should work just fine.


I think i got it now!

THANK YOU!

--
Lord Gas, the Waxen - Blasfemo di IFTMDG - Penguin's Enemy #1
[ICQ: 57483751][the Waxen Pith: http://www.snipp.org/gas]
.... "Goddamn, there's always such a big temptation to be good"
.... (Tom Waits - God's Away on Business)
 
Reply With Quote
 
Tom St Denis
Guest
Posts: n/a
 
      07-01-2003
Lord Gas, the Waxen wrote:
> Greetings, you horrible lot. Here is the last warp from Tom St
> Denis
>
>
>>simple code

>
> [cut]
>
>>Should work just fine.

>
>
> I think i got it now!
>
> THANK YOU!


No prob, glad to help provided you understood why I chose fgets/sscanf

Tom

 
Reply With Quote
 
Jason Xie
Guest
Posts: n/a
 
      07-01-2003
> while(1){
> printf("\nPress '1' to read messages\nPress '2' to write a
> message\nPress '3' to delete a message\nPress any other key to
> exit\n");


//change as this:

do
{
scanf("%d",&scelta);
}while(d<1 && d>4)
switch(scelta)
{
case 1: reader(); break;
case 2: writer(); break;
case 3: rubber(); break;
case 4:
default:
ExitProcess(0);
}
}

--
Jason
The world is a huge family, Love each other.


 
Reply With Quote
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      07-01-2003
In 'comp.lang.c', "Lord Gas, the Waxen" <(E-Mail Removed)> wrote:

>> Have you looked into what scanf returns?

>
> it returns the number of variables... but, i mean, %d means


Wrong. If your C-book says that, burn it, and get a true one.

www.accu.org

*scanf() returns the number of successful conversions. Very different and
useful!

--
-ed- (E-Mail Removed) [remove YOURBRA before answering me]
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
<blank line>
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
 
Reply With Quote
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      07-01-2003
In 'comp.lang.c', "Lord Gas, the Waxen" <(E-Mail Removed)> wrote:

>> Which is why you should use fgets/sscanf.

>
> i did as you told, but it doesn't work ((


Please post the non working code. I think we can fix it!

--
-ed- (E-Mail Removed) [remove YOURBRA before answering me]
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
<blank line>
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
catch doesn't catch a thrown exception Marteno Rodia Java 5 08-05-2009 03:30 AM
catch(...) doesn't catch everything Adam C++ 9 02-02-2006 05:02 PM
why catch (...) can not catch such exception John Black C++ 8 08-20-2004 02:34 PM
Trace: Can anyone suggest a good tool to catch trace messages? Rukmal Fernando ASP .Net 4 10-27-2003 09:03 PM



Advertisments