Second instance of scnaf not working

Discussion in 'C Programming' started by raseelbhagat, Aug 9, 2008.

  1. raseelbhagat

    raseelbhagat Guest

    Hi,
    I am writing a simple program in which I am using scnaf() twice. The
    pseudo code of the program is as follows :

    ....
    printf("Enter lesson no.:");
    scanf("%d",lesson);
    ...
    fp = fopen("questions.txt","r");
    while(fgets(buf, sizeof(buf), fp) {
    fputs(buf, stdout);
    }
    fclose(fp);
    printf("Choose answer\n");
    scanf("%c", &c);
    if (c == 'A')
    printf("Right Answer\n");
    else
    pritnf("Wrong answer\n");
    .....

    In the above code, if I don't use the first scanf(), everything works
    properly.
    Otherwise, after entering an int for the first scanf, the program just
    "falls through" without waiting for user input for the second scanf()
    and printing "Wrong Answer".

    Am I missing out on something here ?
     
    raseelbhagat, Aug 9, 2008
    #1
    1. Advertisements

  2. raseelbhagat

    raseelbhagat Guest


    An additional progress to my above conundrum is : Substituting "%s"
    with "%c" in the second scanf() solves the problem.
    But I don't understand, shouldn't "%c" have worked too ?
     
    raseelbhagat, Aug 9, 2008
    #2
    1. Advertisements

  3. raseelbhagat

    vippstar Guest

    add fflush(stdout); after printf here.
    change this to %4d and check the return value of scanf
    if((rc = scanf("%4d", lesson)) != 1)

    by the way, is 'lesson' really a pointer to int, or plain int? Latter
    case, change it to &lesson.
    if(fp == NULL) /* handle error */
    Most probably what you have entered before in the first scanf is this:

    42<RET>

    The <RET> leaves a newline in the stream. the first scanf sees this
    and stops reading the integer, leaving the newline in the stream.
    Then the second scanf reads the newline instead of waiting for input.
    To fix this, before the second scanf, add these two lines:

    scanf("%*[^\n]");
    getchar();

    <snip rest code and questions>
     
    vippstar, Aug 9, 2008
    #3
  4. raseelbhagat

    vippstar Guest

    On Aug 9, 3:44 pm, "" <>
    wrote:
    See my other reply. Before the second scanf, the stream contains a
    newline.
    's' ignores all isspace bytes. (\n, ' ', \t ...)
     
    vippstar, Aug 9, 2008
    #4
  5. raseelbhagat

    CBFalconer Guest

    Yes. First, each scanf should have the result checked. Also, each
    should be followed by absorbing the remainder of the input line.
    Change the first section to:

    printf("Enter lesson no.:"); fflush(stdout);
    if (1 != scanf("%d",lesson))
    badinputerrorhandle();
    else {
    flushln(stdin);
    (* success, continue *)

    and the very useful flushln function is:

    /* flush input through the next following '\n' inclusive */
    int flushln(FILE *f) {
    int ch;

    while (('\n' != (ch = getc(f)) && (EOF != ch)) continue;
    return ch;
    }

    This treatment should be used on any interactive use of scanf.
     
    CBFalconer, Aug 9, 2008
    #5
    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.