title: C++ discard the leftover newline from input buffer issue
body: <p>I am trying to get this assignment down for a c++ class.  I had issues with the do/while loop not working correctly and someone suggested adding the line cin.ignore(2,'\n'); in the InputData function under where the enter student name is being asked by the user.  That worked and the do/while is now working.  However, I'm not 100% sure how the cin.ignore(2,'\n'); works and I have an issue during the first go around where the first two characters of the "name" that the user inputs is getting discarded.  If I change that 2 to a 0 it it doesn't cut off the first two characters of the name but if the user enters 'y' they'd like to continue, the program skips the first question "Enter the name of the student".</p>
  
  <p>Any help is greatly appreciated!!</p>
  
  <p>FYI, I am super new to programming in general, especially c++.  Be nice please lol.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cstdlib&gt;
  #include &lt;string&gt;
  using namespace std;
  
  class Student {
  public:
     Student();
     ~Student();
     // Input all info of user
     void InputData();
     // Output class list
     void OutputData();
     // Reset class list
     void ResetClasses();
     Student&amp; operator =(const Student&amp; rightSide);
  
  private:
      string name;
      string stuName;
      int numbClasses;
      string *classList;
  };//end student class
  
  //Initialize variables to empty and array to NULL
  Student::Student() {
      numbClasses = 0;
      classList = NULL;
      name = "";
  }//end variable initialization
  
  //Frees up any memory allocated to array.
  Student::~Student() {
      if (classList != NULL) {
      delete [ ] classList;
  }//end if
  }//end free memory
  
  //Delete the class list
  void Student::ResetClasses() {
      if (classList != NULL) {
         delete [] classList;
         classList = NULL;
      }//end if block
      numbClasses = 0;
  }//end reset classes
  </code></pre>
  
  <p>Here is where the line cin.ignore(2,'\n'); is located</p>
  
  <pre><code>// Inputs info from user.
  void Student::InputData() {
  
  int i;
  // Reset the class list in case method is called again and array isn't cleared
  ResetClasses();
  
  cout &lt;&lt; "Enter student name." &lt;&lt; endl;
  //Discards the leftover newline from input buffer
  cin.ignore(2,'\n');
  getline(cin, name);
  
  
  cout &lt;&lt; "Enter number of classes." &lt;&lt; endl;
  cin &gt;&gt; numbClasses;
  //Discards the leftover newline from input buffer
  cin.ignore(2,'\n');
  if (numbClasses &gt; 0) {
      // Construct array big enough to hold # of classes
      classList = new string[numbClasses];
      // Loop through the # classes, input name of each one into array
      for (i = 0; i &lt; numbClasses; i++) {
          cout &lt;&lt; "Enter name of class " &lt;&lt; (i+1) &lt;&lt; endl;
          getline(cin, classList[i]);
      }//end for loop
  }//end if block
  cout &lt;&lt; endl;
  }//end input data
  </code></pre>
  
  <p>Output data</p>
  
  <pre><code>//Output info entered by user.
  void Student::OutputData() {
  
  int i;
  cout &lt;&lt; "Name: " &lt;&lt; name &lt;&lt; endl;
  cout &lt;&lt; "Number of classes: " &lt;&lt; numbClasses &lt;&lt; endl;
  for (i=0; i&lt;numbClasses; i++) {
      cout &lt;&lt; "  Class " &lt;&lt; (i+1) &lt;&lt; ":" &lt;&lt; classList[i] &lt;&lt; endl;
  }//end for loop
  cout &lt;&lt; endl;
  }//end Output data
  
  //overload this operator so there aren't two references to same class list.
  Student&amp; Student::operator =(const Student&amp; rightSide) {
  
  int i;
  // Erase list of classes
  ResetClasses();
  name = rightSide.name;
  numbClasses = rightSide.numbClasses;
  
  // Copy the list of classes
  if (numbClasses &gt; 0) {
      classList = new string[numbClasses];
      for (i=0; i&lt;numbClasses; i++) {
          classList[i] = rightSide.classList[i];
      }//end for loop
  }//end if block
  return *this;
  }//end overload
  </code></pre>
  
  <p>Main, where the do/while loop is located.</p>
  
  <pre><code>// main function
  int main() {
  
      char choice;
      //Do/While loop to ask user if they'd like to continue or end program.
  do {
      // Test code with two student classes
      Student s1, s2;
      // Input for s1
      s1.InputData();
      cout &lt;&lt; "Student 1's data:" &lt;&lt; endl;
      // Output for s1
      s1.OutputData();
      cout &lt;&lt; endl;
  
      s2 = s1;
      cout &lt;&lt; "Student 2's info after assignment from student 1:" &lt;&lt; endl;
      // Should output same info as student 1
      s2.OutputData();
  
      s1.ResetClasses();
      cout &lt;&lt; "Student 1's info after the reset:" &lt;&lt; endl;
      // Should have no classes
      s1.OutputData();
  
      cout &lt;&lt; "Student 2's info, should still have original classes:" &lt;&lt; endl;
      // Should still have original classes
      s2.OutputData();
      cout &lt;&lt; endl;
  
      cout &lt;&lt; "Would you like to continue? y/n" &lt;&lt; endl;
      cin &gt;&gt; choice;
  
  } while(choice == 'y'); //end do/while
  return 0;
  }//end main
  </code></pre>