title: Keep getting a segmentation fault after tried to clear the buffer?
body: <p>Hi I was writing a simple code to help my self understand function pointer, but then I ran into another problem trying to clear the buffer because of the "\n" character being carried over, I have tried attempting to clear the buffer but ran into a another problem when i run the code i get a segmentation fault  when the else statement is true, why is this? can someone guide me in a way I can clear the buffer or a better method of handling this please and thank you.</p>
  
  <pre><code>               /*
                  *      funcptrs.c
                  *
                  *      Program to demonstrate the use of function pointers
                  *
                  *      by Mvitagames
                  */
                  #include &lt;stdio.h&gt;
                  #include &lt;stdlib.h&gt;
  
                  static void goodbye() {
  
                    printf("\nPress ENTER to exit: ");
                    fflush(stdin);
                    getchar();
  
                  }
  
                  static int add(int a, int b) { return a + b;}
  
                  static int subtract (int a, int b) { return a - b;}
  
                  int main() {
                    int  i, j;
                    int  result;
                    int  (*func_ptr)(int , int );
                    int ch;
                    char buf[BUFSIZ];
  
                    atexit(goodbye);
  
                    printf("Please enter the first number: ");
                    scanf("%d", &amp;i);
                    printf("Please enter the second number: ");
                    scanf("%d", &amp;j);
  
  
                    while ((ch = getchar()) != '\n' &amp;&amp; ch != EOF);
  
                    printf("Would you like to add or subtract (a/s)? ");
                    if (getchar() == 'a')
                      func_ptr = add;
                    else
                      //printf("I got here");
                      func_ptr = subtract;
  
                     result = func_ptr(i,j);
  
                     printf("The result is %d\n", result);
  
  
                     return (0);
                    }
  </code></pre>