title: Is there any way to make a C++ Switch Statement loop back to the first case?
body: <p>OK, here is a simple code example:</p>
  
  <pre><code>    char answer;
      cin &gt;&gt; answer;
      switch(answer)
      {
          case 'y':
          case 'Y':
              inventory[0] = "Short Sword";
              cout &lt;&lt; "\nYou place the Rusty Battle Axe in the chest.";
              break;
  
          case 'n':
          case 'N':
              inventory[0] = "Rusty Battle Axe";
              cout &lt;&lt; "\nYou leave the Short Sword in the chest.";
              break;
  
          default :
              cout &lt;&lt; "\nThat was an invalid response.";
      }
  </code></pre>
  
  <p>Obviously I could pull my hair out with a while(answer != 'Y' || answer !=...) But is there a more elegant way of simply returning to the first case after executing the default case? So if a user enters the wrong letter, I simply ask them the question again until they type an acceptable response?</p>
  
  <p>No this isn't homework or anything. I'm working through Dawson's C++ Game Programming book, and I wanted to jazz up the program example a little by allowing the user to keep or trade an item. I got all that working beautifully, but if a wrong response is entered it just shows the contents of the inventory and exits. I wanted to get that right. Force the user to enter a correct response, then show the updated inventory afterwards.</p>
  
  <p>Appreciate the help!</p>
  
  <p><strong>UPDATE!</strong>
  You have all given me so many different approaches to this - I really appreciate it! I admit I probably did not design this switch statement correctly and I apologize for the contradiction. I will try each of your suggestions and post back here, choosing one as answer. Thank you!</p>
  
  <p>OK, I have just gone through all of your answers, trying most of them with my code. I have chosen the simplest, most elegant solution as the answer to my question. But you all have helped me to see different ways of looking at this, and I understand so much more about switch statements now. Using it in fact in place of a while loop in a tutorial I am following right now at YouTube by user What's A Creel?</p>
  
  <p>I really appreciate all your help! I feel that I have really accomplished a lot in my programming practice today. You guys (and gals) are all awesome!</p>
  
  <p><strong>UPDATED AND COMPLETE CODE:</strong></p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  
  using namespace std;
  
  // This program displays a hero's inventory
  
  int main()
  {
      const int MAX_ITEMS = 4;
      string inventory[MAX_ITEMS];
  
      int numItems = 0;
      inventory[numItems++] = "Rusty Battle Axe";
      inventory[numItems++] = "Leather Armor";
      inventory[numItems++] = "Wooden Shield";
  
      cout &lt;&lt; "Inventory:\n";
      for(int i = 0; i &lt; numItems; ++i)
      {
          cout &lt;&lt; inventory[i] &lt;&lt; endl;
      }
  
      cout &lt;&lt; "\nYou open a chest and find a Lesser Healing Potion.";
      inventory[numItems++] = "Lesser Healing Potion";
  
      cout &lt;&lt; "\nInventory\n";
      for(int i = 0; i &lt; numItems; ++i)
      {
          cout &lt;&lt; inventory[i] &lt;&lt; endl;
      }
  
      cout &lt;&lt; "\nYou also find a Short Sword.";
      if(numItems &lt; MAX_ITEMS)
      {
          inventory[numItems++] = "Short Sword";
      }
      else
      {
          cout &lt;&lt; "\nYou have too many items and can't carry another.";
          cout &lt;&lt; "\nWould you like to trade the " &lt;&lt; inventory[0] &lt;&lt; " for the Short Sword? ";
      }
  
      while (true)
      {
          char answer;
          cin &gt;&gt; answer;
          switch(answer)
          {
              case 'y':
              case 'Y':
                  inventory[0] = "Short Sword";
                  cout &lt;&lt; "\nYou place the Rusty Battle Axe in the chest." &lt;&lt; endl;
                  break;
  
              case 'n':
              case 'N':
                  inventory[0] = "Rusty Battle Axe";
                  cout &lt;&lt; "\nYou leave the Short Sword in the chest." &lt;&lt; endl;
                  break;
  
              default:
                  cout &lt;&lt; "\nThat was an invalid response!";
                  cout &lt;&lt; "\nWould you like to trade the " &lt;&lt; inventory[0] &lt;&lt; " for the Short Sword? ";
                  continue;
          }
          break;
      }
  
      cout &lt;&lt; "\nInventory:\n";
      for(int i = 0; i &lt; numItems; ++i)
      {
          cout &lt;&lt; inventory[i] &lt;&lt; endl;
      }
      return 0;
  }
  </code></pre>