title: In C - scanf() in a for loop is causing printf() to run multiple times
body: <p>I'm completing an assignment and after completing it, I have 1 bug, and 1 bug fix I made that I don't fully understand. Currently, as long as the user does what is asked, everything works fine. But I know that doesn't happen often, so I'd love to know how to stop these issues.
  Would love any advice - I am a complete beginner with C.</p>
  
  <p>I found many different pieces of advice here: <a href="https://stackoverflow.com/questions/9562218/c-multiple-scanfs-when-i-enter-in-a-value-for-one-scanf-it-skips-the-second-s#comment12121953_9562355">C: Multiple scanf&#39;s, when I enter in a value for one scanf it skips the second scanf</a></p>
  
  <p>I added a space to my scanf() statements which solved some of the bugs - and I understand that \n is added onto the end of the entered strings / chars, I'm just not sure how to check for it / handle it, and I tried using getchar() in place of the scanf() but I still get double print / loop problems. </p>
  
  <p><strong>Bug Issue</strong></p>
  
  <p>When the user is running through the game loop, if they enter more than 1 character (for example: 'oo', when prompted with the scanf() to enter 'y' or 'n') my printf statements run 1x per character entered, and connect to each other:</p>
  
  <p>Example would be:</p>
  
  <blockquote>
    <p>Welcome to Two doors.</p>
    
    <p>Would you like to play? (y/n):Welcome to Two doors.</p>
    
    <p>Would you like to play? (y/n):</p>
  </blockquote>
  
  <p>This issue also shows up if the user enters 'y' to play the game but then enters a character other than 1,2 or 3 in the second section.</p>
  
  <p>How can I limit the length of their response? Or is the best way to monitor the length of the play and choice variables prior to entering the if statements? Maybe checking to see if they are longer than 1 character and if so, only taking the first character?</p>
  
  <p>Second issue - bug fix that I don't understand 
  In the scanf() functions I ran into a very similar problem to what I described above, but it happened when the user entered any character. The solution I found was to add a space before the character -></p>
  
  <pre><code>scanf(" %c", &amp;play);
  </code></pre>
  
  <p>vs</p>
  
  <pre><code>scanf("%c", &amp;play);
  </code></pre>
  
  <p>Is this issue only a problem when using loops? Since I never found these bugs prior to looping back through the code.</p>
  
  <p>Updated Code with 'while (getchar() != '\n');' suggestion from Sourav Ghosh</p>
  
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;stdio.h&gt;
  
  int main(void) {
  
      char play;
      int choice;
      char answer[] = "No matter which one you choose the guards both tell you which door leads to death, and therefore you can pick the other door.\n";
      int gameLoop = 1;
      int timesPlayed = 0;
  
      while (gameLoop == 1){
  
          if (timesPlayed == 0) {
              printf("Welcome to Two doors.\n");
              printf("Would you like to play? (y/n):");
          } else {
              printf("Would you like to play again? (y/n):");
          }
          scanf(" %c", &amp;play);
          while (getchar() != '\n');
  
          if (play == 'y') {
              // == instead of =
              printf("\nYou are a prisoner in a room with 2 doors and 2 guards.\n");
              printf("One of the doors will guide you to freedom and behind the other is a hangman --you don't know which is which.\n");
              printf("One of the guards always tells the truth and the other always lies. You don't know which one is the truth-teller or the liar either.\n");
              printf("You have to choose and open one of these doors, but you can only ask a single question to one of the guards.\n");
              printf("What do you ask so you can pick the door to freedom?\n\n");
              printf("\t1.Ask the truth-guard to point to the door of doom.\n");
              printf("\t2.Ask the liar-guard to point to the door of doom.\n");
              printf("\t3.Doesn't matter which one you pick.\n");
              scanf(" %d", &amp;choice);
              while (getchar() != '\n');
  
              switch (choice) {
  
                  case 1:
                  printf("%s", answer);
                  timesPlayed++;
                  break;
  
                  case 2:
                  printf("%s", answer);
                  timesPlayed++;
                  break;
  
                  case 3:
                  printf("%s", answer);
                  timesPlayed++;
                  break;
  
                  default:
                  printf("The Troll Smasher comes out from the shadows and squeezes the stupid out of you until you pop. GAME OVER!\n");
                  break;
              }
          } else if(play == 'n') {
              printf("Sorry to hear that, we at Two Doors hope you have a super duper day!\n");
              gameLoop = 0;
              break;
          } else {
              printf("That is not a valid input, please try again by entering either 'y' to start the game or 'n' to quit the game.\n");
          }
      }
      return 0;
  
  }
  </code></pre>