title: cin.getline messing up in for loop
body: <p>Ok I'm trying to do this program where I have to let the user enter students names followed by their test score. I'm trying to set it up so I'll have an array of names and an array of grades. I'm having trouble adding the names to the array. I tried to call cin.getline() inside the for loop and assign that to each subscript in the array. However, it fails miserably. Can somebody point me in the right direction?</p>
  
  <pre><code>#include&lt;iostream&gt;
  #include&lt;iomanip&gt;
  #include&lt;string&gt;
  using namespace std;
  
  //Function prototypes
  void sort(double*, int);
  double average(double*, int);
  void drop(double*, int);
  
  int main()
  {   
      char ch;
      char name[30];
      int numTestScores;
      string *studentNames;
      double *testScorePtr;
      double testAverage;
  
      //Get the number of test scores.
      cout &lt;&lt; "\nHow many test scores will you enter? ";
      cin &gt;&gt; numTestScores;
  
      //Validate the input.
      /*while (numTestScores &lt; 0)
      {
          cout &lt;&lt; "The number cannot be negative.\n";
          cout &lt;&lt; "Enter another number: ";
          cin &gt;&gt; numTestScores;
      }*/
  
      // Allocate an array to hold the test scores
      studentNames = new string[numTestScores];
      testScorePtr = new double[numTestScores];
  
      //Fill the array with test scores.
      for(int i = 0; i &lt; numTestScores; i++)
      {
  
  
          cout &lt;&lt; "Enter name and test score for test # "
          &lt;&lt; (i+1) &lt;&lt; " : "&lt;&lt; endl;
          cin.getline(name,30);
          studentNames[i] = name;
          cout &lt;&lt; studentNames[i] &lt;&lt;endl; //I tried to use this to see if the names were going into the array
  
  
  
  
  
  
          cin.get();
      }
  
          //Get a test score.
          //cout &lt;&lt; "Enter test score "
              //&lt;&lt; (i + 1) &lt;&lt; ": ";
          //cin &gt;&gt; testScorePtr[i];
  
          //Validate the input.
          /*while (numTestScores &lt; 0)
          {
              cout &lt;&lt; "Negative scores are not allowed.\n";
              cout &lt;&lt; "Enter another score for this test: ";
              cin &gt;&gt; testScorePtr[i];*/
  
  
  
  
  
  
      // Sort the test scores.
      sort(testScorePtr, numTestScores);
  
  
  
      //Display the resulting data.
      cout &lt;&lt; "\nThe test scores in ascending "
          &lt;&lt; "order, and their average, are:\n\n";
      cout &lt;&lt; "Score" &lt;&lt;endl;
      cout &lt;&lt; "----" &lt;&lt;endl;
  
      for (int j = 0; j &lt; numTestScores; j++)
      {
          cout &lt;&lt; "\n" &lt;&lt; fixed &lt;&lt; setprecision(2)
               &lt;&lt; setw(6) &lt;&lt; testScorePtr[j];
      }
  
      //Drop The Lowest Grade
      drop(testScorePtr, numTestScores);
  
  
  // Get the average of the test scores.
      testAverage = average(testScorePtr, numTestScores);
  
  
      cout &lt;&lt; "\n\nAverage score: " &lt;&lt; setprecision(2) &lt;&lt; fixed &lt;&lt; testAverage &lt;&lt;endl &lt;&lt;endl;
  
  //  Free the dynamically-allocated memory.
      delete [] testScorePtr;
      testScorePtr = 0;
  
      return 0;
  }
  
  
      //****************************************
      //Function sort
      //This function performs a selection sort
      //on the array pointed to by the score
      //parameter into ascending order. The size
      //parameter holds the number of elements.
      //*****************************************
  
      void sort(double* score, int size)
      { 
        int startScan, minIndex;
        double minValue;
  
        for (startScan = 0; startScan &lt; (size - 1); startScan++)
        {
            minIndex = startScan;
            minValue = score[startScan];
            for(int index = startScan + 1; index &lt; size; index++)
            {
                if (score[index] &lt; minValue)
                {
                    minValue = score[index];
                    minIndex = index;
                }
            }
            score[minIndex] = score[startScan];
            score[startScan] = minValue;
        }
      }
  
      void drop(double* score, int size)
      {
  
          score[0] = 0;
      }
  
      //*************************************************
      //Function average
      //This function calculates and returns the 
      //average of the values stored in the array
      //passed into the scores parameter. The
      //parameter numScors holds the number of elements in the array
  
      double average(double* score, int numScores)
      {
          double total = 0; //Accumulator
          numScores--;
          //Calculate the total of the scores.
          for (int k = 0; k &lt;= numScores ; k++)
          {
              total += score[k];
          }
          //Return the average score.
          return (total/numScores);
      }
  </code></pre>