title: getchar() doesn't actually take input from user
body: <p>I am writing a flexible command-line (but not for long!) diamond-square generator in C++. I have just finished writing the user input half. However, on the very last command, input "slips" and a newline is automatically inputted to getchar(). I have taken precautions to ensure that it's not any sort of overflow, namely, fflushing both stdin, and, for good measure, stdout. The problem persists. Here is my code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  int main () {
      unsigned  long seed = 0, x = 0, y = 0, initial = 0, range = 0;
      int smooth = 0, fail = 1;
      char flagchar1 = 'n';
      printf("Welcome to my diamond-square generator! This isn't full-feature yet, so I'm just gonna have you input the variables one by one. ");
      do {
          printf("Please input the seed (this is a positive integer):\n");
          fail = scanf("%lu", &amp;seed);
          while (fail == 0) {
              printf("Try again, smartass.\n");
              fail = scanf("%lu", &amp;seed);
          }
          fail = 1;
          printf("Now input the x, or horizontal, size of your grid:\n");
          fail = scanf("%lu", &amp;x);
          while (fail == 0) {
              printf("An integer. Not a string. An integer. You can do that, can't you?\n");
              fail = scanf("%lu", &amp;x);
          }
          fail = 1;
          printf("Now input the y, or vertical, size of your grid:\n");
          fail = scanf("%lu", &amp;y);
          while (fail == 0) {
              printf("What was that supposed to be? An integer, please.\n");
              fail = scanf("%lu", &amp;y);
          }
          fail = 1;
          printf("Now input about how high you'd like the grid to be (this goes from a scale of 1 to 256):\n");
          fail = scanf("%lu", &amp;initial);
          while (initial == 0 || initial &gt; 256 || fail == 0) {
              printf("ahahahahaha how HIGH do you have to be just to HAVE that hieght........\n");
              fail = scanf("%lu", &amp;initial);
          }
          fail = 1;
          printf("Now input the range of the heights on your grid (this must be equal to or less than 256):\n");
          scanf("%lu", &amp;range);
          while (range &gt;= 256 || fail == 0) {
              printf("What did I say about being equal to or less than 256? Give me something reasonable to work with here.\n");
              fail = scanf("%lu", &amp;range);
          }
          fail = 1;
          printf("Just one more variable to go! Now, I need you to input the smoothness of your grid. Smaller numbers make spikier grids. You can make this negative, but beware!\n");
          fail = scanf("%d", &amp;smooth);
          while (fail == 0) {
              printf("That... was not a number.\n");
              fail = scanf("%d", &amp;smooth);
          }
          fail = 1;
          printf("\nOkay. Are these the values you want?\n   Seed:       %lu\n   Width:      %lu\n   Length:     %lu\n   Height:     %lu\n   Range:      %lu\n   Smoothness: %d\nDo you want to keep these? Type Y/n.\n", seed, x, y, initial, range, smooth);
          fflush(stdin);
          fflush(stdout);
          flagchar1 = getchar();
      } while (flagchar1 != 'y' &amp;&amp; flagchar1 != 'Y' &amp;&amp; flagchar1 != '\n');
  }
  </code></pre>
  
  <p>Here is my output, the program having ended (the program just repeats the entire do-while loop if I remove the <code>&amp;&amp; flagchar1 != '\n'</code> from <code>while()</code>):</p>
  
  <pre>
      Welcome to my diamond-square generator! This isn't full-feature yet, so I'm just gonna have you input the variables one by one. Please input the seed (this is a positive integer):
      12345678
      Now input the x, or horizontal, size of your grid:
      40
      Now input the y, or vertical, size of your grid:
      30
      Now input about how high you'd like the grid to be (this goes from a scale of 1 to 256):
      1288
      ahahahahaha how HIGH do you have to be just to HAVE that hieght........
      128
      Now input the range of the heights on your grid (this must be equal to or less than 256):
      30
      Just one more variable to go! Now, I need you to input the smoothness of your grid. Smaller numbers make spikier grids. You can make this negative, but beware!
      10
  
  Okay. Are these the values you want?
    Seed:       12345678
    Width:      40
    Length:     30
    Height:     128
    Range:      30
    Smoothness: 10
  Do you want to keep these? Type Y/n.
  </pre>
  
  <p>What's happening, and how do I fix it?</p>
  
  <p>P.S. I know my input validation is essentially useless. Help with this is also greatly appreciated.</p>