title: Baseball Batting Average Program
body: <p>I need help with a program for school.  We had to write a program that asks the user for information about a baseball player.  We need to calculate the players batting average with their games played, number of times at bat and number of hits.  I am running into an issue where my computation for the average is outputting a set number and not performing any computations.  I am entering whole integers for all the variables that are used for calculation.  So i would input numbers like 1, 4 , 10 etc...  As the program stands the value my formula is setting itself equal to is 15903.876.  All of my variables used for the average formula are declared as integers and the batting average itself is declared as a double.  I have done some debugging my self and found that the computation messes up when it divides the number of times at bat by the number of hits.  If anyone could help me figure out the issue i would appreciate it.</p>
  
  <pre><code>//libaries
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;sstream&gt;
  #include &lt;iomanip&gt; 
  using namespace std;
  
  class battingAverage
  {
  public:
  
    string pName;
    int nBats;
    int tHits;
    int gPlayed;
    int gcalled;
    double average;
    double average1;
    double playeraverage;
  
  };
  
  int main()
  {
     string numberPlayers;
    int nplayers;
   //enters the number of players the user wants to enter data for
   cout &lt;&lt; "Enter the number of players you want to enter data for: ";
   cin &gt;&gt; numberPlayers;
   cout &lt;&lt; endl;
  
  //converts the value of numberPlayers to nplayers
  istringstream convert(numberPlayers);
  
  //sets integer nplayers equal to the value of the string numberPlayers
  if(! (istringstream(numberPlayers) &gt;&gt; nplayers) )
  {
      nplayers = 0;
  }
  
  cout &lt;&lt; "This program calculates the batting average of baseball players.\nYou may enter data for " &lt;&lt; nplayers &lt;&lt; " players." &lt;&lt; endl;
  
  battingAverage ba[nplayers];
  
  int index = 0;
  
  //while statement to get data
  while(index &lt; nplayers)
  {
    cout &lt;&lt; "Enter the players last name: " &lt;&lt; endl;
    cin &gt;&gt; ba[index].pName;
  
    cout &lt;&lt; "Enter the number of games the player played: " &lt;&lt; endl;
    cin &gt;&gt; ba[index].gPlayed;
    cout &lt;&lt; ba[index].gPlayed &lt;&lt; endl;
  
    cout &lt;&lt; "Enter the number of games the player was called in for: " &lt;&lt; endl;
    cin &gt;&gt; ba[index].gcalled;
    cout &lt;&lt; ba[index].gcalled &lt;&lt; endl;
  
    cout &lt;&lt; "Enter the number of times the player was at bat: " &lt;&lt; endl;
    cin &gt;&gt; ba[index].nBats;
    cout &lt;&lt; ba[index].nBats &lt;&lt; endl;
  
    cout &lt;&lt; "Enter the number of time the player hit: " &lt;&lt; endl;
    cin &gt;&gt; ba[index].tHits;
    cout &lt;&lt; ba[index].tHits &lt;&lt; endl;
  
    if(ba[index].tHits &gt; ba[index].nBats)
    {
      cout &lt;&lt; "Enter a valid value for the number of times the player hit: ";
      cin &gt;&gt; ba[index].tHits;
    }
  
    cout &lt;&lt; endl;
    index++;
  
  }
  
  //rounds average to 3 decimal places
  cout &lt;&lt; fixed &lt;&lt; setprecision( 3 );
  //average formula
  ba[index].playeraverage = (ba[index].nBats / ba[index].tHits) * (ba[index].gPlayed / ba[index].gcalled);//error
  cout &lt;&lt; ba[index].playeraverage &lt;&lt; endl &lt;&lt; endl;//just temp line to check calculation of average.
  
  ba[index].average = .000;
  ba[index].average1 = .099;
  
   while(ba[index].average &lt; 1 &amp;&amp; ba[index].average1 &lt; .899)
  {
      ba[index].average +=.100;
      ba[index].average1 += .1;
      //prints chart
      cout &lt;&lt; setprecision( 1 ) &lt;&lt; ba[index].average  &lt;&lt; "00" &lt;&lt; setprecision( 3 ) &lt;&lt; setw(12) &lt;&lt; ba[index].average1 &lt;&lt; endl;
      }
  
  cout &lt;&lt; "1.000" &lt;&lt; setw(12) &lt;&lt; "1.000" &lt;&lt; endl;
  
  //version of system pause
  cout &lt;&lt; "\nPress enter to continue...";
  cin.sync();
  cin.ignore();
  return 0;
  </code></pre>
  
  <p>}</p>