how to clear bufferi fflush(stdin) is not working

This is a discussion on how to clear bufferi fflush(stdin) is not working within the C Programming forums, part of the General Programming Boards category; Code: #include<stdlib.h> #include<stdio.h> #include<string.h> struct word_l { char word[100]; char meaning[100]; struct word_l * next; }; typedef struct word_l word; ...

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    5

    Lightbulb how to clear bufferi fflush(stdin) is not working

    Code:
    #include<stdlib.h>
    #include<stdio.h>
    #include<string.h>
    struct word_l {
       char word[100];
       char meaning[100];
       struct word_l * next;
    };
    typedef struct word_l word;
    
    
    void addNode(word *);
    void displayNode(word *);
    char choose(void);
    
    
    int main() 
    {
       char choice;
       word *head;
       head = (word *)malloc(sizeof(word));
       head->next=NULL;
       choice=choose();
       switch(choice)
       {
       case 'a':
             addNode(head);
             break; 
       case 'e':
             exit(0); 
       }
       displayNode(head);
    exit(0);
    }
    void addNode(word *head)
    {
     printf("Enter word and Meaning and at End enter NO\n");
     printf("Word :-");
     gets(head->word);
     printf("Meaning:-") ;
     gets(head->meaning);
     fflush(stdin);
     if(strcmp(head->word,"NO")==0)
        head->next=NULL;
     else
     {
     head->next=(word *)malloc(sizeof(word));
     addNode(head->next);
     }
    return;
    }
    
    
    void displayNode(word *head)
    {
       printf("\v-----------------------------------------------\n");
       while(head->next->next) {
          printf("%10s:- %s\n", head->word,head->meaning);
          head = head->next ;
       }
       printf("\v-----------------------------------------------\n");
    }
    
    
    char choose(void)
    {
       char  choice;
       printf("a)Recursion\ne)exit\nChoice:-");
       choice=getchar();
       return choice;
    }

    output is

    a)Recursion
    e)exit
    Choice:-a
    Enter word and Meaning and at End enter NO
    Word :-Meaning:-
    Enter word and Meaning and at End enter NO
    Word :-apple
    Meaning:-apple

    Expected output is
    a)Recursion
    e)exit
    Choice:-a
    Enter word and Meaning and at End enter NO
    Word :-
    Meaning:-
    Enter word and Meaning and at End enter NO
    Word :-apple
    Meaning:-apple

  2. #2
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    Check the FAQ that you agreed to read when you signed up to this forum?

    FAQ > Why fflush(stdin) is wrong - Cprogramming.com

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    2,700

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    1. Avoid casting the return value from your malloc calls. The malloc function returns a void pointer that can be safely assigned without need for a cast.
    2. Avoid using the gets function. It is susceptible to buffer overruns. Instead prefer the fgets function which accepts a size parameter.
    3. The getchar function returns an int, not a char. In some cases this is not important but in others it can be a critical distinction.



    Now, on to your issue. As hinted at in the link provided above by ledow, the fflush function is only defined for use with output streams (such as stdout). Since stdin is an input stream, not an output stream, its use in your code is therefore representative of undefined behavior. It may work or it may not, it all depends on what your compiler does with it. As such, it is definitely not portable. You should strive to write code that is as portable as possible and there are always alternatives which obviate the need to use such a crutch.

    In your case, the problem is that there is a stray newline in the input stream after processing the users choice. When your program reaches the gets function, the newline is seen as an empty input string and consumed. The program continues past the I/O for "Word" and progresses to "Meaning" without allowing you to enter the necessary text for "Word". Even if your use of fflush were to be well-defined behavior, your placement of it in your posted code sample would still be incorrect. You would have to put it before that first gets call, not after the second one.

    Now, there are many portable ways of dealing with the stray newline that's causing your problem. One of the easiest would be to place a second getchar call in your choose function after the first one. This second call would just ignore the return value.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie Question - fflush(stdin) & fpurge(stdin) on Mac and PC
    By tvsinesperanto in forum C Programming
    Replies: 34
    Last Post: 03-11-2006, 11:13 AM
  2. fflush(stdin)
    By mako in forum C Programming
    Replies: 23
    Last Post: 01-21-2006, 02:12 PM
  3. fflush(stdin);
    By ygfperson in forum C++ Programming
    Replies: 2
    Last Post: 08-11-2002, 06:23 PM
  4. fflush(stdin)
    By RyeDunn in forum C Programming
    Replies: 24
    Last Post: 07-18-2002, 09:07 PM
  5. ascii 10 in the stdin after fflush(stdin)??
    By Kev2 in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 06-03-2002, 03:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21