Thread: Scanf

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    9

    Scanf

    Why does scanf sometimes fails like in this example the 2nd scanf was ignored.


    include <stdio.h>
    #include <conio.h>


    main()
    {
    int x;
    char y;
    float a;

    scanf("%i",&x);
    scanf("%c",&y);
    scanf("%f",&a);

    printf("%i %c %f",x,y,a);



    }

    Thanks

  2. #2
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    Isn't scanf in a differen't header file?

    Also, at the mercy of any potential replies to your message, please use code tages and make sure your code's syntax is exactly what you're trying to compile.
    The world is waiting. I must leave you now.

  3. #3
    Registered User Dohojar's Avatar
    Join Date
    Feb 2002
    Posts
    115
    It is because scanf() reads the enter key after you enter first input number. it reads the number and puts it into x but then it goes on to read that enter key stroke and puts it into c. Then when you enter a char it will try to put it into the float value.
    you should do this:

    scanf("%i ",&x);
    scanf("%c",&y);
    scanf("%f",&a);

    note the space after the %i in the first scanf. That will eat the enter key for you so it will work right.
    Dohojar Moajbuj
    Time is the greatest teacher, too bad it kills all its students

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    11
    I think that Shadow is correct, the only header file I see being properly used is <conio.h>.

  5. #5
    Registered User biosx's Avatar
    Join Date
    Aug 2001
    Posts
    230

    flush the buffer

    He doesn't even need conio.h. He is using all stdio.h functions.

    Another way to fix the scanf problem is to flush the stdin buffer after each reading so the next scanf will have a fresh, empty buffer:
    Code:
    #include <stdio.h>
    
    int main()
    {
       int i;
       char c;
       float f;
    
       scanf("%d", &i);
       fflush(stdin);
    
       scanf("%c", &c);
       fflush(stdin);
    
       scanf("%f", &f);
       fflush(stdin);
    
       printf(" %d - %c - %f ", i, c, f);
    
       return 0;
    }
    Good luck!

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    When reading string data, scanf does take a newline character as valid input, so when you used scanf the last time for numeric data the newline was left in the buffer and the next call to scanf that accepted a newline as valid scanned it in.

    A newline is also considered whitespace by scanf, which is delimited by whitespace. So it appeared as if the call was skipped when it was in fact working properly.

    >Another way to fix the scanf problem is to flush the stdin buffer after each reading
    >fflush(stdin);
    Wrong, using fflush in input streams results in undefined behavior which is a big no-no. I'll try to be nice, but please do not answer questions if all you can teach is bad habits biosx, I've already corrected you on this point several times. Here's how to flush the input stream safely:
    Code:
    #include <stdio.h>
    
    int main ( void )
    {
       int i, c;
       scanf("%d", &i);
       while ( getchar() != '\n' );
       scanf("%c", &c);
       printf("Integer: %d\nCharacter: %c\n", i, c);
       return 0;
    }
    -Prelude
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    9
    Can I still use fflush function if the case is just an alternative to getch() function, cause if Ill be using linux OS conio.h library is not available.

    can I use

    assuming

    char dummy;

    fflush(stdin);
    scanf("%c",&dummy);


    instead of getch();

    Thanks prelude

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Can I still use fflush function if the case is just an alternative to getch() function
    Here's the thing, if fflushing input streams is undefined in one case then it's undefined in ALL cases. Don't use fflush ( stdin ), don't use it ever. You can use fflush as long as it's on output streams though.

    If I had the choice of using something nonstandard, using something standard but not quite what I want, and using something undefined then I would go with standard. Undefined is never to be used, nonstandard only when you must, and standard whenever you can.

    -Prelude
    My best code is written with the delete key.

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    9
    Can I use then getchar as an alternative as you have given previously? Is this function also available in linux OS for C.

    Thanks
    Prelude

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Can I use then getchar as an alternative as you have given previously?
    Yes

    >Is this function also available in linux OS for C
    getchar() is a standard function. If you search www.google.com then you can find a list of the standard functions. Linux has nothing to do with the availability of functions, that is the sole duty of the compiler.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf() consideres useless
    By Snafuist in forum C Programming
    Replies: 15
    Last Post: 02-18-2009, 08:35 AM
  2. Help with a basic scanf procedure.
    By killpoppop in forum C Programming
    Replies: 9
    Last Post: 11-03-2008, 04:39 PM
  3. Replies: 2
    Last Post: 02-20-2005, 01:48 PM
  4. Scanf and integer...
    By penny in forum C Programming
    Replies: 3
    Last Post: 04-24-2003, 06:36 AM
  5. scanf - data is "put back" - screws up next scanf
    By voltson in forum C Programming
    Replies: 10
    Last Post: 10-14-2002, 04:34 AM