title: C++ Validation - Truncation set as next input
body: <p>I consistently run into this problem where I'm trying to validate user input but I find myself using tons of ignores() throughout my program. Since it is a <strong>school program</strong> I'm only <strong>limited</strong> to just <strong>iostream</strong>, <strong>cctype</strong>, and <strong>student.h</strong> libraries. The problem is I need to make sure the user does not try to enter alpha characters into an integer field which I thought I covered with <code>if(!(cin &gt;&gt; val))</code> and then I use <code>cin.ignore(numeric_limits&lt;streamsize&gt;::max(), '\n');</code> to ignore anything extra (for instance if they try to enter a decimal into an integer field). Something is not working correctly though as I am either unable to input into name field or I get the remaining decimal as the name (if I enter 123.6, the name will be .6). Does someone know a better way to validate integers than using mass ignores?</p>
  
  <p><strong>Main</strong></p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  #include "student.h"
  //**************************************************************************
  bool validInt(int&amp;);
  //**************************************************************************
  int main()
  {
      Student student;
      bool validInput;
  
      cout &lt;&lt; "You have Chosen to Add a New Student." &lt;&lt; endl;
      cout &lt;&lt; "---------------------------------------" &lt;&lt; endl;
  
      // Student ID Validation
      do
      {
          cout &lt;&lt; "Enter Student ID (ex. 123): ";
          validInput = validInt(student.id);
      }while(!validInput);
  
      cout &lt;&lt; "Enter Student Name (ex. John Doe): ";
      cin.getline(student.name, 50);
  
      cout &lt;&lt; "Enter Student City and State (ex. St. Louis, Missouri): ";
      cin.getline(student.citystate, 50);
  
      cout &lt;&lt; "\n\nStudent ID: " &lt;&lt; student.id &lt;&lt; endl;
      cout &lt;&lt; "Student Name: " &lt;&lt; student.name &lt;&lt; endl;
      cout &lt;&lt; "Student City / State: " &lt;&lt; student.citystate &lt;&lt; endl;
  
      return 0;
  }
  //**************************************************************************
  bool validInt(int&amp; val)
  {
      bool valid = true;
      if(!(cin &gt;&gt; val))
      {
          cout &lt;&lt; "\nERROR: Please enter a Positive Whole Number" &lt;&lt; endl;
          cin.clear();
          cin.ignore(numeric_limits&lt;streamsize&gt;::max(), '\n');
          valid = false;
      }
      return valid;
  }
  </code></pre>
  
  <p><strong>Student Header</strong></p>
  
  <pre><code>#ifndef STUDENT_H
  #define STUDENT_H
  //**************************************************************************
  struct Student
  {
      int id;
      char name[50];
      char citystate[50];
  
      friend ostream&amp; operator&lt;&lt;(ostream&amp; out, const Student&amp; data);
  
      bool operator == (const Student &amp;rhs) const;
      bool operator != (const Student &amp;rhs) const;
      bool operator &lt; (const Student &amp;rhs) const;
      bool operator &gt; (const Student &amp;rhs) const;
      bool operator &lt;= (const Student &amp;rhs) const;
      bool operator &gt;= (const Student &amp;rhs) const;
  };
  //**************************************************************************
  ostream&amp; operator &lt;&lt; (ostream&amp; out, const Student&amp; data)
  {
      out &lt;&lt; data.id &lt;&lt; " " &lt;&lt; data.name &lt;&lt; endl;
      return out;
  }
  //**************************************************************************
  bool Student::operator == (const Student &amp;rhs) const
  {
      return (this-&gt;id == rhs.id);
  }
  //**************************************************************************
  bool Student::operator != (const Student &amp;rhs) const
  {
      return (this-&gt;id != rhs.id);
  }
  //**************************************************************************
  bool Student::operator &lt; (const Student &amp;rhs) const
  {
      return (this-&gt;id &lt; rhs.id);
  }
  //**************************************************************************
  bool Student::operator &gt; (const Student &amp;rhs) const
  {
      return (this-&gt;id &gt; rhs.id);
  }
  //**************************************************************************
  bool Student::operator &lt;= (const Student &amp;rhs) const
  {
      return (this-&gt;id &lt;= rhs.id);
  }
  //**************************************************************************
  bool Student::operator &gt;= (const Student &amp;rhs) const
  {
      return (this-&gt;id &gt;= rhs.id);
  }
  #endif
  </code></pre>