title: C++ Average Calculation Function Returning 0
body: <p>I'm writing a program that calculates the batting averages of a user defined number of players. The program displays the name of a player, their number of times at bat, number of hits, and their batting average. Finally, it displays the total number of times the players were at bat, the total number of hits, and the overall average. For some reason, the functions that calculate the individual player average and overall average are returning 0. It's probably something small, but I'm stumped as to how to try and fix it.</p>
  
  <pre><code>//Batting Average Calculator
  
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;iomanip&gt;
  using namespace std;
  
  //Create Structure
  struct Record
  {
      string name;
      int AB;
      int hit;
      double avg;
  };
  
  
  int getSize(int);
  void getData(Record[], int );
  int calculateTotalAB(Record[], int, int);
  int calculateTotalHit(Record[], int, int);
  double calculateTotalAvg(Record[], int, double);
  void calculateAvg(Record[], int);
  void display(Record[], int, int , int, double);
  
  
  int main()
  {
      const int MaxSize = 50;
      Record players[MaxSize]; 
  
      int size = 0;
      int totalAB = 0;
      int totalHit = 0;
      double totalAvg = 0;
  
      size = getSize(size);
      getData(players, size);
      totalAB = calculateTotalAB(players, size, totalAB);
      totalHit = calculateTotalHit(players, size, totalHit);
      calculateAvg(players,size);
      totalAvg = calculateTotalAvg(players, size, totalAvg);
      display(players, size, totalHit, totalAB, totalAvg);
  }
  
  //get number of players to be calculated
  int getSize(int size)
  {
      cout &lt;&lt; "Please enter the number of players on the team: ";
      cin &gt;&gt; size;
      return size;
  }
  
  //get Player name, AB, and hit
  void getData(Record players[], int size)
  {
      string dummy;
      getline(cin, dummy);
      for (int i = 0; i &lt; size; i++)
      {
          cout &lt;&lt; "Please input the name of student " &lt;&lt; i + 1 &lt;&lt; ": ";
          getline(cin, players[i].name);
          cout &lt;&lt; "Please input the number of times "&lt;&lt;  players[i].name &lt;&lt; " was at bat:  ";
          cin &gt;&gt; players[i].AB;
          cout &lt;&lt; "Please input the number of hits for " &lt;&lt; players[i].name &lt;&lt; ": ";
          cin &gt;&gt; players[i].hit;
          cout &lt;&lt; " " &lt;&lt; endl;
          getline(cin, dummy);
      }
  }
  
  int calculateTotalAB(Record players[], int size, int totalAB)
  {
  
      for (int i = 0; i &lt; size; i++)
      {
          totalAB = totalAB + players[i].AB;
      }
      return totalAB;
  }
  
  int calculateTotalHit(Record players[], int size, int totalHit)
  {
  
      for (int i = 0; i &lt; size; i++)
      {
          totalHit = totalHit + players[i].hit;
      }
      return totalHit;
  }
  
  void calculateAvg(Record players[], int size)
  {
      for (int i = 0; i &lt; size; i++)
      {
          players[i].avg = players[i].hit / players[i].AB;
      }
  }
  
  double calculateTotalAvg(Record players[], int size, double totalAvg)
  {
      double j = 0;
      for (int i = 0; i &lt; size; i++)
      {
          j = j + players[i].avg;
      }
      totalAvg = j / size;
      return totalAvg;
  }
  
  void display(Record players[], int size, int totalHit, int totalAB, double totalAvg)
  {
      cout &lt;&lt; fixed &lt;&lt; showpoint &lt;&lt; setprecision(3);
      cout &lt;&lt; "Player        AB         Hit         Avg" &lt;&lt; endl;
      cout &lt;&lt; "  " &lt;&lt; endl;
      for (int i = 0; i &lt; size; i++)
      {
          cout &lt;&lt; players[i].name &lt;&lt; setw(8) &lt;&lt; players[i].AB &lt;&lt; setw(5) &lt;&lt; players[i].hit &lt;&lt; setw(5) &lt;&lt; players[i].avg;
      }
      cout &lt;&lt; "  " &lt;&lt; endl;
      cout &lt;&lt; "Totals      " &lt;&lt; totalAB &lt;&lt; "      " &lt;&lt; totalHit &lt;&lt; "      " &lt;&lt; totalAvg &lt;&lt; endl;
  }
  </code></pre>