title: Why does the program exit, without waiting for the user input?
body: <p>This C program is supposed to allocate memory based on number of elements entered by user and add them and print the result. It again prompts if user wants to add more numbers or not. But while entering Y/N the console closes and the program ends unexpectedly. How to solve this ?</p>
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  int main()
  {
      int n,i;
      int *ptr,*old_ptr;
      int sum = 0;
      char a;
  
      printf(&quot;Enter the number of elements to be added: &quot;);
      scanf(&quot;%d&quot;,&amp;n);
  
      ptr = calloc(n,sizeof(int));
      ptr = old_ptr;
  
      printf(&quot;Enter the elements:\n&quot;);
  
      for(i = 0;i &lt; n;i++){
          scanf(&quot;%d&quot;,ptr);
          sum = sum + *ptr;
          ptr++;
      }
      printf(&quot;The sum total of the numbers is: %d\n\n\n&quot;,sum);
  
      printf(&quot;Do you want to enter more numbers ?\nPress Y/N: &quot;);
      scanf(&quot;%c&quot;,&amp;a);
      if(a == 'Y'){
          printf(&quot;\n\nYou have entered: %c\n\n&quot;,a);
          ptr = realloc(old_ptr,sizeof(int)*n);
  
          printf(&quot;Enter the elements:\n&quot;);
  
      for(i = 0;i &lt; n;i++){
          scanf(&quot;%d&quot;,&amp;ptr);
          sum = sum + *ptr;
          ptr++;
      }
      printf(&quot;The total of the numbers is: %d\n\n\n&quot;,sum);
      }
      if(a == 'N'){
          printf(&quot;Program finished!&quot;);
      }
      return 0;
  }
  </code></pre>