Thread: fgets skipping, buffer issue?

  1. #1
    Registered User
    Join Date
    Feb 2011

    fgets skipping, buffer issue?

    my code is here
    void putbook(libr *lib, int cnt)
    int i;
    long unsigned int sal;
            printf("You will be adding information for %d books.\n", cnt);
            for (i=0; i<cnt; i++)
                    printf("\nFor book #%d Enter the access No: ", (i+1));
                    printf("Enter the author's name(lastname, firstname):");
                    fgets((*lib).name, 100, stdin);
                    printf("%s", (*lib).name);
                    printf("Enter the books information:\nTitle:\n");
                    fgets((*lib).book.title, 100, stdin);
                    printf("%s", (*lib).book.title);
                    scanf("%f", &(*lib).book.price);
                    scanf("%d", &(*lib).book.year);
                    printf("Enter the books status (Available, Not_Available, Reference_Only, Online): ");
                    scanf("%d", &(*lib).Status);
    my first fgets is skipped, i think because i'm using scanf before gets and it leaves a \n behind in the keyboard buffer, i've tried all types of buffer clearing, any suggestions on how I can get my fgets to stop skipping??
    thank you.

  2. #2
    Registered User
    Join Date
    Jan 2009
    Have you tried:

    while( getchar() != '\n' );

    after scanf() ?

  3. #3
    Registered User
    Join Date
    Feb 2011
    I actually fixed this problem by just putting getchar(); after every scanf that precedes a fgets.
    Thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fgets returns null
    By strider1974 in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 05:08 AM
  2. fgets and skipping a line...
    By pollypocket4eva in forum C Programming
    Replies: 3
    Last Post: 01-03-2009, 01:42 PM
  3. Simple (?) problem rendering a vertex buffer
    By Dark_Phoenix in forum Game Programming
    Replies: 4
    Last Post: 08-11-2007, 07:32 PM
  4. getline problem
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 10-06-2001, 09:28 AM
  5. Does anyone Know How to..?
    By kwigibo in forum C Programming
    Replies: 12
    Last Post: 09-20-2001, 08:16 AM