Thread: scanf problems

  1. #1
    Unregistered
    Guest

    scanf problems

    I am trying to write a program for my C programming class and I cannot get scanf("%c") to work. The program doesn't wait for the user to enter a character before continuing. If someone could please help me soon I would be so happy. the program si due in 3 hours.

    Thank you so much!

  2. #2
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192

    Thumbs up

    Hi,
    try including the statement

    fflush(stdin);


    before the scanf() statement and I guess ur problem should be solved.
    Regards,

  3. #3
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    522
    Hi,
    try including the statement

    fflush(stdin);
    NO, the result of fflush is undefined for input streams.
    Use:
    Code:
    while(getchar() != '\n');
    A better idea would be to never use scanf again.
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  4. #4
    Registered User PutoAmo's Avatar
    Join Date
    Mar 2002
    Posts
    72
    Dear Unregistered,

    Problem is that scanf() will read from standard input (most often keyboard) until it finds a newline character (the 'enter' key). When it finally finds the newline character, it doesn't take it with the rest of the data to be formatted !! Instead, it is left in the input stream !! When next scanf() in program is processed it will read that newline character left from former scanf() and put it in the first variable in the variable list. Exemple :

    Code:
    scanf ("%d", &a_number);
    
    scanf ("%c", &a_character);
    In this case, the newline charater by first scanf() in input buffer will be put in a_character by second scanf(). We can avoid this inconvenience by means of a space character in format string in second scanf(), right befor %c. Or we can just use a function, like getchar() that "eats" that newline character.

    Code:
    scanf ("%d", &a_number);
    
    scanf (" %c", &a_character);    /* space character before %c */
    or ...

    Code:
    scanf ("%d", &a_number);
    
    while (getchar() != '\n');   /* usually implemented this way */
    
    scanf ("%c", &a_character);

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >We can avoid this inconvenience by means of a space character in format string in second scanf(),
    Another alternative is to place a newline in the format string of the first call to scanf:
    Code:
    scanf ( "%d\n", &i );
    scanf ( "%c", &ch );
    printf ( "%d %c\n", i, ch );
    The number of ways to get around the bugs alone should teach you not to use scanf, it's just plain dangerous if you don't know how it works.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with Calculating
    By danepporambo in forum C Programming
    Replies: 6
    Last Post: 04-25-2006, 01:04 AM
  2. Replies: 2
    Last Post: 02-20-2005, 01:48 PM
  3. scanf to struct pointer
    By ronenk in forum C Programming
    Replies: 11
    Last Post: 12-20-2004, 10:22 AM
  4. First scanf() skips next scanf() !
    By grahampatten in forum C Programming
    Replies: 5
    Last Post: 08-17-2004, 02:47 AM