title: How do you limit the maximum amount of characters in user input in C++?
body: <p>I want it so that when the user inputs more than 5 characters, something will happen, instead of just skipping the rest.</p>
  
  <p>In this code, if you type in more than 5 characters, it will only show the first 5 characters.
  I want to put an "if" statement here that if the user inputs more than 5 characters, it will show an error or something, and not just show the first 5 characters.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;iomanip&gt;
  
  int main()
  {
  using namespace std;
  string nationname;
  int maxchar = 5;
  cout &lt;&lt; "What is the name of your nation?\n";
  
  cin &gt;&gt; setw(maxchar) &gt;&gt; nationname;
  
  cout &lt;&lt; "The name of your nation is " &lt;&lt; nationname;
  cin.ignore();
  
  return 0;
  }
  </code></pre>
  
  <p>Thanks!</p>