Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

How to catch this exception?

 
 
A. Sinan Unur
Guest
Posts: n/a
 
      07-01-2003
Tom St Denis <(E-Mail Removed)> wrote in news:616Ma.25872$2ay.13520
@news01.bloor.is.net.cable.rogers.com:

> Lord Gas, the Waxen wrote:


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

>
> It pushes the data back on the stream, which is of course invalid
> again. Common for MSVC.


Incidentally, this had nothing whatsoever to do with MSVC. The OP's
problem is addressed in the FAQ list:

http://www.eskimo.com/~scs/C-faq/q12.19.html

as well as:

http://www.eskimo.com/~scs/C-faq/q12.20.html

Both the OP and Tom St Denis would be well advised to peruse this
resource.

--
A. Sinan Unur
http://www.velocityreviews.com/forums/(E-Mail Removed)
Remove dashes for address
Spam bait: (E-Mail Removed)
 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      07-01-2003
"Jason Xie" <(E-Mail Removed)> wrote in news:bdqvo3$d3d$1
@bcarh8ab.ca.nortel.com:

>> 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)


Where did d come from?

--
A. Sinan Unur
(E-Mail Removed)
Remove dashes for address
Spam bait: (E-Mail Removed)
 
Reply With Quote
 
 
 
 
Jason Xie
Guest
Posts: n/a
 
      07-01-2003
The world is a huge family, Love each other.
"A. Sinan Unur" <(E-Mail Removed)> 写入消息新闻
:Xns93AAF41F366C1asu1cornelledu@132.236.56.8...
> "Jason Xie" <(E-Mail Removed)> wrote in news:bdqvo3$d3d$1
> @bcarh8ab.ca.nortel.com:
>
> >> 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)

>
> Where did d come from?


Sorry, just "scelta"

jason

>
> --
> A. Sinan Unur
> (E-Mail Removed)
> Remove dashes for address
> Spam bait: (E-Mail Removed)



 
Reply With Quote
 
Tom St Denis
Guest
Posts: n/a
 
      07-01-2003
Jason Xie wrote:
> The world is a huge family, Love each other.
> "A. Sinan Unur" <(E-Mail Removed)> 写入消息新闻
> :Xns93AAF41F366C1asu1cornelledu@132.236.56.8...
>
>>"Jason Xie" <(E-Mail Removed)> wrote in news:bdqvo3$d3d$1
>>@bcarh8ab.ca.nortel.com:
>>
>>
>>>>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)

>>
>>Where did d come from?

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

 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      07-01-2003
In <wx6Ma.26275$(E-Mail Removed) ogers.com> Tom St Denis <(E-Mail Removed)> writes:

>char buf[100];
>int x;
>
>while (fgets(buf, sizeof(buf)-1, stdin) != NULL) {

^^^^^^^^^^^^^
Where does the -1 come from?

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: (E-Mail Removed)
 
Reply With Quote
 
Arthur J. O'Dwyer
Guest
Posts: n/a
 
      07-01-2003

On Tue, 1 Jul 2003, Tom St Denis wrote:
>
> Jason Xie wrote:
> > "A. Sinan Unur" <(E-Mail Removed)> [wrote:]
> >
> >>"Jason Xie" <(E-Mail Removed)> wrote...
> >>
> >>>>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)
> >>
> >>Where did d come from?

> >
> > Sorry, just "scelta"


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
 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      07-01-2003
In <(E-Mail Removed)> "Arthur J. O'Dwyer" <(E-Mail Removed)> writes:


>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
--
Dan Pop
DESY Zeuthen, RZ group
Email: (E-Mail Removed)
 
Reply With Quote
 
Tom St Denis
Guest
Posts: n/a
 
      07-01-2003
Arthur J. O'Dwyer wrote:
> On Tue, 1 Jul 2003, Tom St Denis wrote:
>
>>Arthur J. O'Dwyer wrote:
>>
>>>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?

>>
>>In most implementations, nothing. An integer is not matched and the
>>function exits before doing anything. Which means scetla holds the
>>original value it had.

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

>>Which is why, for the love of god and everything holy on this Earth,
>>people recommend that you use fgets/sscanf with proper return checking.

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

 
Reply With Quote
 
Arthur J. O'Dwyer
Guest
Posts: n/a
 
      07-01-2003

On Tue, 1 Jul 2003, Dan Pop wrote:
>
> "Arthur J. O'Dwyer" <(E-Mail Removed)> writes:
> >
> >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).


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
 
Reply With Quote
 
Lefteris Kororos
Guest
Posts: n/a
 
      07-01-2003
Dan Pop wrote:
> In <wx6Ma.26275$(E-Mail Removed) ogers.com> Tom St Denis <(E-Mail Removed)> writes:
>
>
>>char buf[100];
>>int x;
>>
>>while (fgets(buf, sizeof(buf)-1, stdin) != NULL) {

>
> ^^^^^^^^^^^^^
> Where does the -1 come from?
>
> Dan

You need to account for the NULL terminator of the string.

Lefteris

 
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