How to catch this exception?

  • Thread starter Lord Gas, the Waxen
  • Start date
L

Lord Gas, the Waxen

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
 
A

A. Sinan Unur

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?
 
L

Lord Gas, the Waxen

Greetings, you horrible lot. Here is the last warp from A. Sinan
Unur
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
 
L

Lord Gas, the Waxen

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

Arthur J. O'Dwyer

Greetings, you horrible lot. Here is the last warp from A. Sinan Unur

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
 
L

Lord Gas, the Waxen

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! :D

THANK YOU! :D
 
T

Tom St Denis

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! :D

THANK YOU! :D

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

Tom
 
J

Jason Xie

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);
}
}
 
E

Emmanuel Delahaye

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!
 
T

Tom St Denis

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



Sorry, just "scelta" :)

That isn't much better, consider the following two lines of input

line1: 1
line2: tom is so good

At the second line scelta == 1 so the do/while will break with the
program thinking that 1 was the users choice.

Tom
 
A

Arthur J. O'Dwyer

The "correction" changes the meaning of the program (that is,
changes its design, not just fixes bugs). So even if it were
a correct "correction", it would be unhelpful unless you (Jason)
provide some reason *why* the change should be made.
Note that in your version, only the input '4' will stop the
program (intentionally). The prompt to the user doesn't
give any clue that this is the case. That's bad.
That isn't much better, consider the following two lines of input

line1: 1
line2: tom is so good

At the second line scelta == 1 so the do/while will break with the
program thinking that 1 was the users choice.

I had thought that arguments to scanf that couldn't be matched received
indeterminate values (and thus line 1 wasn't even necessary), but it
appears I was wrong. Could anyone provide chapter and verse on what
happens to 'scelta' during the second scanf call?

-Arthur
 
D

Dan Pop

I had thought that arguments to scanf that couldn't be matched received
indeterminate values (and thus line 1 wasn't even necessary), but it
appears I was wrong. Could anyone provide chapter and verse on what
happens to 'scelta' during the second scanf call?

Is there anything preventing yourself for reading the specification of
scanf?

4 The fscanf function executes each directive of the format in
turn. If a directive fails, as detailed below, the function
returns.
....
16 The fscanf function returns the value of the macro EOF if an
input failure occurs before any conversion. Otherwise, the
function returns the number of input items assigned, which can
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
be fewer than provided for, or even zero, in the event of an
early matching failure.

The function only performs an assignment upon a successful conversion
(unless the respective directive contained the assignment suppression
character).

Dan
 
T

Tom St Denis

Arthur said:
That is what I think, too. But I can't find anything to support that
view except the apparent lack of explicit contradiction in N869.
It uses the term "assignment" to describe how 'scelta' would receive
its value, were it to receive one, implying that the value is not to be
changed otherwise, but that isn't very clear IMHO.

It probably doesn't say this in the spec but I think its reasonable to
assume that if it hasn't found the type desired in the stream/string
that is value is undefined [e.g. check the return of *scanf].
Use of fgets() has nothing to do with use of scanf(). The two are
different functions that do different, but related, things.

scanf() won't flush the input though which is the problem.
I don't see the big uphill challenge. Your way bad, my[our] way good.
Your way bad, our way good, ....


Huh? First: what makes you think I advocate scanf over fgets?
Second: what makes you think fgets is better than scanf for reading
integer input?

fgets reads a line of text regardless of whether it contains what you
were looking for. scanf puts the data back on the stream which means
you get endless loops.
-Arthur

int val, p;
input:
printf("Enter an integer.\n");
p = scanf("%d", &val);
if (p != 1) {
if (feof(stdin)) exit(EXIT_FAILURE);
printf("\nThat was not an integer.\n");
while (getchar() != '\n') ;
goto input;
}
printf(" You (eventually) entered %d.\n", val);

This is more complicated than a simple fgets/sscanf

Tom
 
A

Arthur J. O'Dwyer

Is there anything preventing yourself for reading the specification of
scanf?

4 The fscanf function executes each directive of the format in
turn. If a directive fails, as detailed below, the function
returns.
...
16 The fscanf function returns the value of the macro EOF if an
input failure occurs before any conversion. Otherwise, the
function returns the number of input items assigned, which can
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
be fewer than provided for, or even zero, in the event of an
early matching failure.

The function only performs an assignment upon a successful conversion
(unless the respective directive contained the assignment suppression
character).

Yes. I found that. I just wasn't absolutely sure that the function
wouldn't "perform" anything *else* in the event of a matching failure.
Bertrand Mollinier has referred me to one of the examples, which does
seem to indicate that scanf is a no-op if it can't match the appropriate
specifier.

-Arthur
 

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,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top