How to catch this exception?

Discussion in 'C Programming' started by Lord Gas, the Waxen, Jul 1, 2003.

  1. 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)
     
    Lord Gas, the Waxen, Jul 1, 2003
    #1
    1. Advertising

  2. "Lord Gas, the Waxen" <> wrote in
    news::

    > 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

    Remove dashes for address
    Spam bait: mailto:
     
    A. Sinan Unur, Jul 1, 2003
    #2
    1. Advertising

  3. Greetings, you horrible lot. Here is the last warp from A. Sinan
    Unur
    > "Lord Gas, the Waxen" <> wrote in
    > news::
    >
    > > 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)
     
    Lord Gas, the Waxen, Jul 1, 2003
    #3
  4. 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)
     
    Lord Gas, the Waxen, Jul 1, 2003
    #4
  5. 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" <> 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
     
    Arthur J. O'Dwyer, Jul 1, 2003
    #5
  6. 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

    --
    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)
     
    Lord Gas, the Waxen, Jul 1, 2003
    #6
  7. Lord Gas, the Waxen

    Tom St Denis Guest

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


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

    Tom
     
    Tom St Denis, Jul 1, 2003
    #7
  8. Lord Gas, the Waxen

    Jason Xie Guest

    > 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.
     
    Jason Xie, Jul 1, 2003
    #8
  9. In 'comp.lang.c', "Lord Gas, the Waxen" <> 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- [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/
     
    Emmanuel Delahaye, Jul 1, 2003
    #9
  10. Emmanuel Delahaye, Jul 1, 2003
    #10
  11. Tom St Denis <> 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

    Remove dashes for address
    Spam bait: mailto:
     
    A. Sinan Unur, Jul 1, 2003
    #11
  12. "Jason Xie" <> 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

    Remove dashes for address
    Spam bait: mailto:
     
    A. Sinan Unur, Jul 1, 2003
    #12
  13. Lord Gas, the Waxen

    Jason Xie Guest

    The world is a huge family, Love each other.
    "A. Sinan Unur" <> дÈëÏûÏ¢ÐÂÎÅ
    :Xns93AAF41F366C1asu1cornelledu@132.236.56.8...
    > "Jason Xie" <> 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
    >
    > Remove dashes for address
    > Spam bait: mailto:
     
    Jason Xie, Jul 1, 2003
    #13
  14. Lord Gas, the Waxen

    Tom St Denis Guest

    Jason Xie wrote:
    > The world is a huge family, Love each other.
    > "A. Sinan Unur" <> дÈëÏûÏ¢ÐÂÎÅ
    > :Xns93AAF41F366C1asu1cornelledu@132.236.56.8...
    >
    >>"Jason Xie" <> 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
     
    Tom St Denis, Jul 1, 2003
    #14
  15. Lord Gas, the Waxen

    Dan Pop Guest

    In <wx6Ma.26275$> Tom St Denis <> 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:
     
    Dan Pop, Jul 1, 2003
    #15
  16. On Tue, 1 Jul 2003, Tom St Denis wrote:
    >
    > Jason Xie wrote:
    > > "A. Sinan Unur" <> [wrote:]
    > >
    > >>"Jason Xie" <> 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
     
    Arthur J. O'Dwyer, Jul 1, 2003
    #16
  17. Lord Gas, the Waxen

    Dan Pop Guest

    In <> "Arthur J. O'Dwyer" <> 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:
     
    Dan Pop, Jul 1, 2003
    #17
  18. Lord Gas, the Waxen

    Tom St Denis Guest

    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
     
    Tom St Denis, Jul 1, 2003
    #18
  19. On Tue, 1 Jul 2003, Dan Pop wrote:
    >
    > "Arthur J. O'Dwyer" <> 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
     
    Arthur J. O'Dwyer, Jul 1, 2003
    #19
  20. Dan Pop wrote:
    > In <wx6Ma.26275$> Tom St Denis <> 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
     
    Lefteris Kororos, Jul 1, 2003
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. eddie wang

    ASP catch exception

    eddie wang, Jul 24, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    4,431
    eddie wang
    Jul 24, 2003
  2. Franck

    How to catch COM exception in .Net

    Franck, Aug 6, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    546
    Natty Gur
    Aug 7, 2003
  3. John Black
    Replies:
    8
    Views:
    4,163
    Xenos
    Aug 20, 2004
  4. Adam
    Replies:
    9
    Views:
    577
    red floyd
    Feb 2, 2006
  5. Marteno Rodia

    catch doesn't catch a thrown exception

    Marteno Rodia, Aug 3, 2009, in forum: Java
    Replies:
    5
    Views:
    572
    Daniel Pitts
    Aug 5, 2009
Loading...

Share This Page