title: C++ - Creating Basic Combat Function for Text RPG
body: <p>I am currently working on a basic text RPG for a class, and I am having some weird issues going on with the combat system. Basically, I have an option to attack or not to attack the enemy during each combat phase. When I attack the enemy, the health decreases at an expected rate. However, when I choose not to attack (which means that the enemy DOES attack still), the damage I do towards the enemy is not directly stated in that turn, but when I choose to attack the next time, it does the expected damage towards the enemy as well as the damage from when I supposedly did not attack.</p>
  
  <p>(Warning: I know that my code will be poorly written and probably not with the best programming logic... I am a new student, and am just using what I have learned so far.) </p>
  
  <h2>My code is as follows:</h2>
  
  <pre><code>int showEnemyHp(int enemyhp, int attack) {
      enemyhp = enemyhp - attack;
      return enemyhp;
  }
  
  int showHp(int hp, int enemyattack) {
      hp = hp - enemyattack;
      return hp;
  }
  
  void enemyBattle() {
      int hp = 30, enemyhp = 20, attack = 10, enemyattack = 5;
      int hitEnemy;
  
      while (true) {
          cout &lt;&lt; "Hit enemy?\n(1) Yes\n(2) No" &lt;&lt; endl;
          cin &gt;&gt; hitEnemy;
  
          if (hitEnemy == 1) {
              enemyhp = showEnemyHp(enemyhp, attack);
              hp = showHp(hp, enemyattack);
              cout &lt;&lt; "You hit the enemy." &lt;&lt; endl;
              cout &lt;&lt; "The enemy now has " &lt;&lt; enemyhp &lt;&lt; "HP left." &lt;&lt; endl;
              if (enemyhp &lt;= 0) {
                  cout &lt;&lt; "You've killed the enemy!" &lt;&lt; endl;
                  break;
              } else if (enemyhp &gt; 0) {
                  cout &lt;&lt; "The enemy hits you back." &lt;&lt; endl;
                  cout &lt;&lt; "You now have " &lt;&lt; hp &lt;&lt; "HP left." &lt;&lt; endl;
              }
          } else if (hitEnemy == 2) {
              enemyHp = showEnemyHp(enemyhp, attack);
              hp = showHp(hp, enemyattack);
              cout &lt;&lt; "You have chosen not to hit the enemy." &lt;&lt; endl;
              cout &lt;&lt; "The enemy hits you back." &lt;&lt; endl;
              cout &lt;&lt; "You now have " &lt;&lt; hp &lt;&lt; "HP left." &lt;&lt; endl;
          } else {
              cout &lt;&lt; "You can't do that!" &lt;&lt; endl;
              return enemyBattle();
          }
      }
      while (hp &gt; 0 &amp;&amp; enemyhp &gt; 0);
  }
  </code></pre>
  
  <hr>
  
  <p>Can anybody help me with what I am doing wrong, and why that's happening? Also, any hints to fixing such an issue would be great. I would also appreciate possibly any help on how to better optimize this code. (Like I said, my toolbox for C++ is remarkbly limited right now. I am only about a month or so into studying it... my professor has yet to even teach us arrays, which I've learned about on my own). Thank you SO much for any help.</p>