Thread: Problem with getchar()

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    50

    Problem with getchar()

    Hi

    I am using a simple program. First i m asking user to enter three value. I am reading those values in structure and writing the values in file.Then i am asking user whether would like to enter another record. I am reading the option with getchar() funtion. While i am running the program it is not stopping for user input Y or N. After reading first three input, the program is closing.

    Code:
    while(another=='Y')
    {
    another=getchar();
    printf("\n Enter schedule id,schedule name and schedule description\n");
    scanf("%d %s %s",&schinf.sch_id,schinf.sch_name,schinf.sch_dsc);
    printf("%d %s %s",schinf.sch_id,schinf.sch_name,schinf.sch_dsc);
    fwrite(&schinf,sizeof(schinf),1,fp);
    fflush(stdin);
    putchar(another);
    printf("Would you like to enter another record?\n");
    another=getchar();
    
    
    }
    Can anyone tell what could be the reason?

    Thanks
    Sas

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    fflush(stdin) - is undefined - read FAQ
    so after scanf - the \n is left in the input buffer and it is read by getchar
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    50
    Then how i will flush the input buffer? What would be the solution to read a character again?

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You don't flush them out, you PULL them out, with getchar() if all you need is one char removed (normal for a scanf().

    If you have several char's to be removed, the more industrial strength version is:

    Code:
    while((c=getchar()) != '\n');  //note the semi-colon
    
    getchar();
    The while line pulls off all char's until it runs into a newline, and then quits. If you have several keypresses in the keyboard buffer, but just one newline, that's all you need.

    scanf()'s for integers don't need this, because that format skips over whitespace, anyway. The one that needs it the worst is scanf()'s for a single char. If you have those, I just get in the habit of placing getchar()'s after almost every scanf().

    That also stops the problem of scanf()'ing for a number, but the user enters a letter, and you're inside a loop which is waiting for a proper number to be input.

    So now it loops endlessly. With a getchar() in that loop, you'll loop twice, and then stop.

    scanf() is always dodgy for input from a user, but if you have to use it, then getchar() tricks are essential, imo.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  2. getchar() problem
    By jlharrison in forum C Programming
    Replies: 6
    Last Post: 01-25-2006, 02:49 PM
  3. problem with parser code
    By ssharish2005 in forum C Programming
    Replies: 2
    Last Post: 12-02-2005, 07:38 AM
  4. Replies: 5
    Last Post: 11-07-2005, 11:34 PM