title: Xcode C++ How to add a "Play Again?" function to my program
body: <p>my program is all finished but I want to know how to add a "Play again" feature that uses "Yes" or "No" input to decide whether to run the program again or to end it. I am a little unsure of where to place it in my program since I use a handful of while loops and if/else statements. Any insight would be appreciated, thanks! Here is my code:</p>
  
  <pre><code>#include &lt;cstdlib&gt;
  #include &lt;iostream&gt;
  #include &lt;cmath&gt;
  #include &lt;ctime&gt;
  
  using namespace std;
  
  int main() // Setting up the code to follow.
  {
      srand(static_cast&lt;unsigned int&gt;(time(0)));
  
      int Weapon_Selection;  // Creating variables for the player and computer's weapon selection.
      int Comp_Weapon;
      bool Battle = true;    // The variable to begin the battle.
  
      while(Battle)          // The main while loop to run the program code.
      {
          int Player_Health = 100; // More variables for both the player and the computer.
          int Comp_Health = 100;
          int Cannon_Ammo = 3;
          int Grenade_Ammo = 4;
          int Comp_Cannon_Ammo = 3;
          int Comp_Grenade_Ammo = 4;
  
          while(Player_Health &gt; 0 &amp;&amp; Comp_Health &gt; 0) // The while loop to continue running the program while both combatants are alive.
          {
              bool Invalid = true;
              bool Comp_Invalid = true;
  
  
              while(Invalid)
              {
                  cout &lt;&lt; "Select a weapon using number keys 1, 2, and 3.\n"; // Weapon selection input from the player.
                  cout &lt;&lt; "1. Cannon (Ammo left: " &lt;&lt; Cannon_Ammo &lt;&lt; ")\n";
                  cout &lt;&lt; "2. Grenade (Ammo left: " &lt;&lt; Grenade_Ammo &lt;&lt; ")\n";
                  cout &lt;&lt; "3. Rifle\n";
                  cout &lt;&lt; "\n";
                  cout &lt;&lt; "You select: ";
                  cin &gt;&gt; Weapon_Selection;
  
                  srand(static_cast&lt;unsigned int&gt;(time(0)));
  
                  if (Weapon_Selection == 1 &amp;&amp; Cannon_Ammo &gt; 0)
                  {
                      Cannon_Ammo -= 1;
                      Invalid = false;
                      int Player_Dmg = rand() % 5 + 10; // Randomly generated damage range.
                      cout &lt;&lt; "You caused " &lt;&lt;  Player_Dmg &lt;&lt; " cannon damage to your enemy!\n";
                      Comp_Health -= Player_Dmg;
                      cout &lt;&lt; "Your health is " &lt;&lt; Player_Health &lt;&lt; "\n";
                      cout &lt;&lt; "The computer's health is " &lt;&lt; Comp_Health &lt;&lt; "\n";
                      cout &lt;&lt; "\n";
                      break;
                  }
  
                  else if (Weapon_Selection == 2 &amp;&amp; Grenade_Ammo &gt; 0)
                  {
                      Grenade_Ammo -= 1;
                      Invalid = false;
                      int Player_Dmg = rand() % 5 + 7;
                      cout &lt;&lt; "You caused " &lt;&lt; Player_Dmg &lt;&lt; " grenade damage to your enemy!\n";
                      Comp_Health -= Player_Dmg;
                      cout &lt;&lt; "Your health is " &lt;&lt; Player_Health &lt;&lt; "\n";
                      cout &lt;&lt; "The computer's health is " &lt;&lt; Comp_Health &lt;&lt; "\n";
                      cout &lt;&lt; "\n";
                      break;
                  }
  
                  else if (Weapon_Selection == 3)
                  {
                      Invalid = false;
                      int Player_Dmg = rand() % 5 + 3;
                      cout &lt;&lt; "You caused " &lt;&lt; Player_Dmg &lt;&lt; " rifle damage to your enemy!\n";
                      Comp_Health -= Player_Dmg;
                      cout &lt;&lt; "Your health is " &lt;&lt; Player_Health &lt;&lt; "\n";
                      cout &lt;&lt; "The computer's health is " &lt;&lt; Comp_Health &lt;&lt; "\n";
                      cout &lt;&lt; "\n";
                      break;
                  }
  
                  else
                      cout &lt;&lt; "You are out of ammo or you have entered an invalid number." &lt;&lt; "\n"; // Output for wrong number input or no ammo.
                      cout &lt;&lt; "\n";
              }
  
  
              if (Comp_Health &lt;= 0) // Break point if the computer dies in battle.
                  break;
  
              while(Comp_Invalid) // The computer's random weapon selection.
              {
                  Comp_Weapon = rand() % 3 + 1;
  
                  if (Comp_Weapon == 1 &amp;&amp; Comp_Cannon_Ammo &gt; 0)
                  {
                      Comp_Cannon_Ammo -= 1;
                      Comp_Invalid = false;
                      int Comp_Dmg = rand() % 5 + 10;  // Randomly generated damage range for the computer.
                      cout &lt;&lt; "Your enemy used a cannon and caused " &lt;&lt; Comp_Dmg &lt;&lt; " damage to you!\n";
                      Player_Health -= Comp_Dmg;
                      cout &lt;&lt; "Your health is " &lt;&lt; Player_Health &lt;&lt; "\n";
                      cout &lt;&lt; "The computer's health is " &lt;&lt; Comp_Health &lt;&lt; "\n";
                      cout &lt;&lt; "\n";
                  }
  
                  else if (Comp_Weapon == 2 &amp;&amp; Comp_Grenade_Ammo &gt; 0)
                  {
                      Comp_Grenade_Ammo -= 1;
                      Comp_Invalid = false;
                      int Comp_Dmg = rand() % 5 + 7;
                      cout &lt;&lt; "Your enemy used a grenade and caused " &lt;&lt; Comp_Dmg &lt;&lt; " damage to you!\n";
                      Player_Health -= Comp_Dmg;
                      cout &lt;&lt; "Your health is " &lt;&lt; Player_Health &lt;&lt; "\n";
                      cout &lt;&lt; "The computer's health is " &lt;&lt; Comp_Health &lt;&lt; "\n";
                      cout &lt;&lt; "\n";
                  }
  
                  else
                  {
                      Comp_Invalid = false;
                      int Comp_Dmg = rand() % 5 + 3;
                      cout &lt;&lt; "Your enemy used a rifle and caused " &lt;&lt; Comp_Dmg &lt;&lt; " damage to you!\n";
                      Player_Health -= Comp_Dmg;
                      cout &lt;&lt; "Your health is " &lt;&lt; Player_Health &lt;&lt; "\n";
                      cout &lt;&lt; "The computer's health is " &lt;&lt; Comp_Health &lt;&lt; "\n";
                      cout &lt;&lt; "\n";
                  }
  
              }
          }
          if(Player_Health &lt; 0) // End of battle output, regardless of who wins or loses.
              cout &lt;&lt; "You have been defeated by your enemy!" &lt;&lt; "\n" &lt;&lt; endl;
  
          else
              cout &lt;&lt; "Congratulations, you beat your enemy!" &lt;&lt; "\n" &lt;&lt; endl;
  
  
      }
      cin.ignore(2);
      return 0;
  }
  </code></pre>