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, Jul 1, 2003
    #1
    1. Advertisements

  2. What exception?

    Have you looked into what scanf returns?
     
    A. Sinan Unur, Jul 1, 2003
    #2
    1. Advertisements

  3. 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:

    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, Jul 1, 2003
    #3
  4. Greetings, you horrible lot. Here is the last warp from Tom St
    Denis
    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, Jul 1, 2003
    #4
  5. 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.
    Here you call scanf. Where do you check the return value?
    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
    I think i got it now! :D

    THANK YOU! :D
     
    Lord Gas, the Waxen, Jul 1, 2003
    #6
  7. Lord Gas, the Waxen

    Tom St Denis Guest

    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){
    //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 Xie, Jul 1, 2003
    #8
  9. 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!
     
    Emmanuel Delahaye, Jul 1, 2003
    #9
  10. Please post the non working code. I think we can fix it!
     
    Emmanuel Delahaye, Jul 1, 2003
    #10
  11. A. Sinan Unur, Jul 1, 2003
    #11
  12. Where did d come from?
     
    A. Sinan Unur, Jul 1, 2003
    #12
  13. Lord Gas, the Waxen

    Jason Xie Guest

    The world is a huge family, Love each other.
    Sorry, just "scelta" :)

    jason
     
    Jason Xie, Jul 1, 2003
    #13
  14. Lord Gas, the Waxen

    Tom St Denis Guest

    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

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

    Dan
     
    Dan Pop, Jul 1, 2003
    #15
  16. 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.
    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

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

    Tom St Denis Guest

    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].
    scanf() won't flush the input though which is the problem.
    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.
    This is more complicated than a simple fgets/sscanf

    Tom
     
    Tom St Denis, Jul 1, 2003
    #18
  19. 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. You need to account for the NULL terminator of the string.

    Lefteris
     
    Lefteris Kororos, Jul 1, 2003
    #20
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.